对多一位数字或者无数字输入的情况处理都统一了。 1publicclassSolution {2publicint[] plusOne(int[] digits) {3intlen =digits.length;4for(inti=len-1; i>=0; i--) {5intd =digits[i];6if(d<9) {7digits[i]++;8returndigits;9}else{10digits[i] = 0;11}12}13int[] res =newint[len+...
publicclassSolution {publicint[] plusOne(int[] digits) {if(digits ==null) {returnnull; } List<Integer> num =newArrayList<>();intcarry = 0;for(inti = digits.length - 1; i >= 0; i--) {if(i == digits.length - 1) {intsum = (digits[i] + 1) % 10; carry= (digits[i] +...
1 class Solution { 2 public: 3 // not binary digits !!! shit! 4 vector<int> plusOne(vector<int> &digits) { 5 // Note: The Solution object is instantiated only once and is reused by each test case. 6 vector<int> rlt(digits.size()+1,0); 7 int n = digits.size(); 8 rlt[...
publicclassSolution {publicint[] plusOne(int[] digits) {if(digits ==null)returnnull;if(digits[digits.length-1] != 9) { digits[digits.length-1] ++;returndigits; }booleanflag =true;for(inti=0; i<digits.length; i++) {if(digits[i] != 9) { flag=false;break; } }if(flag ==true...
既然如此,只要程序在1过程中没有返回,而运行到了循环外,就说明首位发生了进位,可以不用判断,直接进行步骤2。 具体代码: 1classSolution {2public:3vector<int> plusOne(vector<int> &digits)4{5intn =digits.size();6for(inti=n-1; i>=0; --i)7{8if(digits[i] ==9) {9digits[i] =0;10}else...
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;//留下的值 ...
LeetCode(66)题解: Plus One https://leetcode.com/problems/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....
LeetCode - PlusOne 题意:给一个数按位存放在一个int数组中,要求返回这个数加一后的数组。 懒人解法: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 publicclassSolution { publicint[] plusOne(int[] digits) { java.math.BigInteger Bigdigits =newjava.math.BigInteger(toString...
leetcode 【 Plus One 】python 实现 题目: 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. 代码:oj测试通过 Runtime: 47 ms...
LeetCode(66)——Plus One 技术标签: LeetCode题目: Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit...