C语言
int* plusOne(int* digits, int digitsSize, int* returnSize){ for(int i=digitsSize-1;i>=0;i--){ if(digits[i]==9){ digits[i]=0; } else{ digits[i]++; *returnSize = digitsSize; return digits; } } int* result =(int *) malloc(sizeof(int)*(digitsSize+1)); memset(result, 0, (digitsSize + 1) * sizeof(int)); // set 0 for each bit. result[0]=1; *returnSize = digitsSize+1; return result; }
Python 3
class Solution: def plusOne(self, digits: List[int]) -> List[int]: digits[-1] = digits[-1]+1 for i in range(len(digits)-1,-1,-1): if digits[i]>=10: digits[i] = digits[i]-10 if i==0: digits.insert(0,1) else: digits[i-1] = digits[i-1] + 1 return digits