JasonChang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

use stack

 1 public class Solution {
 2     public int[] plusOne(int[] digits) {
 3         // IMPORTANT: Please reset any member data you declared, as
 4         // the same Solution instance will be reused for each test case.
 5         if(digits == null)
 6             return null;
 7         Stack<Integer> mystack = new Stack<Integer>();
 8         int carry = 1;
 9         int tmp = 0;
10         for(int i = digits.length-1; i >= 0; i--)
11         {
12             tmp = digits[i] + carry;
13             carry = tmp / 10;
14             mystack.push(tmp%10);   
15         }
16         if(carry > 0)
17             mystack.push(1);
18         int[] result = new int[mystack.size()];
19         for(int i = 0; i < result.length;i++)
20             result[i] = mystack.pop();
21         return result; 
22     }
23 }

 

posted on 2013-11-05 16:56  JasonChang  阅读(187)  评论(0编辑  收藏  举报