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

66. Plus One

时间:2017-10-20 21:32:21      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:nta   any   represent   des   i+1   一个   result   含义   表示   

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

标签:nta   any   represent   des   i+1   一个   result   含义   表示   

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

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