鱼C论坛

 找回密码
 立即注册
查看: 1870|回复: 0

[学习笔记] leetcode 66. Plus One

[复制链接]
发表于 2019-8-30 11:26:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

You may assume the integer does not contain any leading zero, except the number 0 itself.

Example 1:

Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Example 2:

Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.

class Solution {
    public int[] plusOne(int[] digits) {
        
        int i = digits.length - 1;
        
        if(digits[i]< 9){
            
            digits[i] = digits[i] + 1;
            
            return digits;
        }
        
        while(digits[i] == 9){
            
            digits[i] = 0;
            
            if(i > 0){
                
               i--; 
            }
            
        }
        if(i == 0 && digits[i] == 0){
            
            digits[i] = digits[i] + 1;
            
            int[] result = new int[digits.length + 1];
            
            int j;
            
            for(j = 0; j< digits.length ; j++){
                
                result[j] = digits[j];
            }
                
                result[j] = 0;
                
                return result;
            
        }
        
        if(i == 0){
            
            digits[i] = digits[i] + 1;
            
            return digits;
            
        }
        
        digits[i] = digits[i] + 1;
        
        return digits;
        
    }
}

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-20 00:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表