Is it a simple code(C++)?


  • 182
    Z
    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);
    		
    }

  • 1
    N

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


  • 0
    L

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


  • 0
    C

    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.


  • 0
    I

    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;
        }
    }

  • 15

    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;
    }
    

  • 0
    I

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


  • 0
    T

    nope it won't


  • 0
    L

    this answer has some problem , 2199 plusone wolud get 3200


  • 1
    E

    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;
    }

  • 0
    M

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


  • 0

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


  • 0
    S

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


  • 0
    S

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


  • 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 :-)


  • 0
    Y

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


  • 0
    Y

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


  • 0

    @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.


  • 1
    H
    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;
    

    }


  • 0
    D

    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;
    }
    

    }


Log in to reply
 

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