leetCode 66.Plus One (+1问题) 解题思路和方法
2017-05-23 09:47
459 查看
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的值。主要就是进位的问题。代码例如以下:
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 { public int[] plusOne(int[] digits) { int k = 1;//进位 for(int i = digits.length - 1; i >=0 ; i--){ digits[i] += k;//加上进位的值 k = digits[i]/10;//进位值 digits[i] %= 10;//留下的值 } //还有进位 if(k > 0){ int[] a = new int[digits.length+1]; a[0] = k; for(int i = 0; i < digits.length; i++){ a[i+1] = digits[i]; } return a; } return digits; } }
相关文章推荐
- leetCode 66.Plus One (+1问题) 解题思路和方法
- leetCode 51.N-Queens (n皇后问题) 解题思路和方法
- leetCode 52.N-Queens II (n皇后问题II) 解题思路和方法
- leetCode 50.Pow(x, n) (x的n次方) 解题思路和方法
- leetCode 83.Remove Duplicates from Sorted List(删除排序链表的反复) 解题思路和方法
- leetCode 60.Permutation Sequence (排列序列) 解题思路和方法
- leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法
- leetCode 33.Search in Rotated Sorted Array(排序旋转数组的查找) 解题思路和方法
- leetCode 87.Scramble String (拼凑字符串) 解题思路和方法
- leetCode 90.Subsets II(子集II) 解题思路和方法
- leetCode 36.Valid Sudoku(有效的数独) 解题思路和方法
- leetCode 39.Combination Sum(组合总和) 解题思路和方法
- leetCode 108.Convert Sorted Array to Binary Search Tree(将排序数组转换为BST) 解题思路和方法
- leetCode 11.Container With Most Water (数轴上容器) 解题思路和方法
- leetCode 78.Subsets (子集) 解题思路和方法
- leetCode 82.Remove Duplicates from Sorted List II (删除排序链表的重复II) 解题思路和方法
- leetcode 114.Flatten Binary Tree to Linked List (将二叉树转换链表) 解题思路和方法
- leetCode 23. Merge k Sorted Lists (合并k个排序链表) 解题思路和方法
- leetCode 69.Sqrt(x) (平方根) 解题思路和方法
- [Leetcode] 66. Plus One 解题报告