java traditional solution


  • 0
    C

    public class Solution {
    public int[] plusOne(int[] digits) {

    	int carry=0;
    	int length = digits.length;
        //对最后一位进行加一操作
    	int sum = 0;
    	//对sum进行判断
        int[] res = new int [length+1];
        
        for(int i=0;i<length;i++)
        {	
        	if(i == 0)
        	{
        		sum = digits[length-1-i]+1+carry;
        	}
        	else 
        	{
        		sum = digits[length-1-i]+carry;	
        	}
        		res[length-i] = sum%10;
        		carry = sum/10;
        }
        res[0] = carry;
        
        if(res[0] == 0)
        {
        	int [] ress = new int [length];
        	for(int j=1;j<length+1;j++)
        	{
        		ress[j-1] = res[j];
        	}
    		return ress;
        }
        
        return res;
    }
    

    }


Log in to reply
 

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