码迷,mamicode.com
首页 > 其他好文 > 详细

66. Plus One

时间:2017-10-14 21:14:06      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:logs   ant   exce   ica   present   return   color   length   self   

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

You may assume the integer do not contain any leading zero, except the number 0 itself.

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

这道题的要求是给定一个数组表示非负整数,其高位在数组的前面,对这个整数加1。

 1     public int[] plusOne(int[] digits) {
 2         int carry  =1;
 3         for (int i=digits.length-1;i>=0;i--)
 4         {
 5             int sum = carry + digits[i];
 6             digits[i] = sum%10;
 7             carry = sum/10;
 8         }
 9         if (carry ==0) return digits;
10         int[] result = new int[digits.length+1];
11         result[0] = carry;
12         for (int i=0;i<digits.length;i++)
13         {
14             result[i+1] = digits[i];
15         }
16         return result;        
17     }

 

66. Plus One

标签:logs   ant   exce   ica   present   return   color   length   self   

原文地址:http://www.cnblogs.com/wzj4858/p/7668438.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!