leetcode解题方案--066--Plus One
2017-12-06 00:34
309 查看
题目
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考虑进位数组长度也有可能变
class Solution { public static int[] plusOne(int[] digits) { int add = 0; int sum = digits[digits.length-1] + 1; digits[digits.length-1] = sum%10; add = sum/10; int index = digits.length-2; while (add!=0 && index>=0) { digits[index] = digits[index]+1; if (digits[index] == 10) { add = 1; digits[index] = 0; } else { add = 0; } index--; } if (add!=0) { int[] ret = new int[digits.length+1]; System.arraycopy(digits, 0, ret,1,digits.length); ret[0] = add; return ret; } return digits; } }
相关文章推荐
- leetcode解题方案--039-- CombinationSum
- leetcode解题方案--054--spiral maxtrix
- leetcode解题方案--011--Container With Most Water
- leetcode解题方案--015--3 sum
- leetcode解题方案--151--Reverse Words in a String
- leetcode解题方案--027--Remove Element
- leetcode解题方案--072--Edit Distance
- leetcode解题方案--166--Fraction to Recurring Decimal
- [Leetcode] 66. Plus One 解题报告
- leetcode 066 —— Plus One
- leetcode解题方案--073--set matrix zeroes
- leetcode解题方案--167--Two Sum II - Input array is sorted
- [LeetCode] Plus One 解题报告
- Java for LeetCode 066 Plus One
- leetcode解题方案--053--Maximum Subarray
- leetcode解题方案--059--Spiral Matrix II
- leetcode解题方案--018--4 sum
- leetcode解题方案--034--Search for a Range
- leetcode解题方案--042--Trapping Rain Water
- 【LeetCode】Plus One 解题报告