首页 > 其他 > 详细

Plus One

时间:2014-11-10 21:42:22      阅读:512      评论:0      收藏:0      [点我收藏+]

Plus One 

Given a non-negative number represented as an array of digits, plus one to the number.

The digits are stored such that the most significant digit is at the head of the list.

 1 public class Solution {
 2     public int[] plusOne(int[] digits) {
 3             digits[digits.length - 1] += 1;
 4             for(int i = digits.length - 1; i >=0 ; i--){
 5                 if((digits[i] / 10) == 1 && i != 0){//有进位
 6                     digits[i] = digits[i] % 10;
 7                     digits[i - 1] += 1;//向高位进1
 8                 }
 9             }
10             if(digits[0] / 10 == 0)
11                 return digits;
12             else{
13                 int result[] = new int[digits.length + 1];
14                 System.arraycopy(digits, 0, result, 1, digits.length);
15                 result[0] = 1;
16                 result[1] %= 10;
17                 return result;
18             }
19        }
20 }

 

Plus One

原文:http://www.cnblogs.com/luckygxf/p/4088188.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!