# Is it a simple code(C++)?

• ``````void plusone(vector<int> &digits)
{
int n = digits.size();
for (int i = n - 1; i >= 0; --i)
{
if (digits[i] == 9)
{
digits[i] = 0;
}
else
{
digits[i]++;
return;
}
}
digits[0] =1;
digits.push_back(0);

}``````

• Perhaps the best solution, it's concise and beautiful.

• really great! How come you think up this elegant solution?

• Nice ! Aren't you supposed to return the array ? Minor detail though...
digits[0] =1; digits.push_back(0); is pretty clever. I came up with .insert(begin(), 1) which is lame.

• This is a Java version using the same idea.

``````public class Solution {
public int[] plusOne(int[] digits) {
int length = digits.length - 1;
while (length >= 0) {
if (digits[length] == 9) {
digits[length--] = 0;
} else {
digits[length]++;
return digits;
}
}
int[] res = new int[digits.length + 1];
res[0] = 1;
return res;
}
}``````

• Nice idea to push 0. Let me just rewrite the loop a bit.

``````vector<int> plusOne(vector<int>& digits) {
for (int i=digits.size(); i--; digits[i] = 0)
if (digits[i]++ < 9)
return digits;
digits[0]++;
digits.push_back(0);
return digits;
}
``````

• Wouldn't this fail for 194? The output should be 195 but it will be 205.

• nope it won't

• this answer has some problem , 2199 plusone wolud get 3200

• Not as smart as zezedi, but does the work:

``````public int[] plusOne(int[] digits) {
int carry = 1;
int[] res = new int[digits.length+1];

for(int i=digits.length-1; i>=0; i--) {
if(digits[i] == 9 && carry == 1) {
res[i] = 0;
carry = 1;
}
else {
res[i] = digits[i] + carry;
carry = 0;
}
}

if(carry==0)
res = Arrays.copyOfRange(res, 0, digits.length);
else
res[0] = carry;

return res;
}``````

• There is no problem with this answer. Take a look at the return statement.

• concise solution! it used the condition 'only plus one' not 2 or more

• Make small change to make it work with empty array.
write digits.push_back(0) before digits[0]++

• Make small change to make it work with empty array.
write digits.push_back(0) before digits[0]=1;

• I'd say that's not necessary, as the empty array isn't the representation of any number. You also cannot write `x = 1 + ;` without the compiler complaining. That said, I still like it, as it's the exact same amount of code and safer in case of a rogue hacker attacking my program :-)

• Could you explain your for loop? why i-- can be written in the second place ? And how digits[i]=0 run in this program?

• If anyone can tell me why this answer failed the runtime when submitted?

• @Yan_Sylvia_Liu The loop simply turns all trailing nines into zeros and if it comes across a non-nine, it increases that and returns. I don't understand your other two questions, sorry.

• ``````vector<int> plusOne(vector<int>& digits) {
vector<int> res;
int carry = 1;
for (int i = digits.size() - 1; i >= 0; i--) {
int sum = carry + digits[i];
carry = sum / 10;
res.push_back(sum % 10);
}
if (carry) res.push_back(1);
reverse(res.begin(), res.end());
return res;
``````

}

• you are very smart, I translate is it to my c code

``````int* plusOne(int* digits, int digitsSize, int* returnSize) {
if(digitsSize < 1)
{
return NULL;
}
int breakflag = 0;
int * result = (int*)malloc(sizeof(int)*(digitsSize+1));
for(int i = digitsSize - 1; i >= 0; i-- )
{
if(digits[i] == 9 && !breakflag)
{
result[i+1] = 0;
}else if(!breakflag)
{
result[i+1] = digits[i]+1;
breakflag = 1;
}else
{
result[i+1] =digits[i];
}
}
if(!breakflag)
{
result[0] = 1;
*returnSize = digitsSize+1;
return result;
}else{
*returnSize = digitsSize;
return result+1;
}
``````

}

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.