Git Product home page Git Product logo

Comments (5)

jeff-liu14 avatar jeff-liu14 commented on July 24, 2024

还有一种解法,先从最后一位加1并往前进位循环一次,然后判断数组首位是否大于等于10 然后确定是否要进位

public int[] plusOne(int[] digits) {
        
        int length = digits.length;
        digits[length -1] = digits[length-1] + 1;
        while(length > 1){
            int tmp = digits[length-1];
            if(tmp >= 10){
                int divide = tmp /10;
                int mod = tmp % 10;
                digits[length-1] = mod;
                digits[length -2] = digits[length-2] + divide;
            }
            length --;
        }
        
        
            int tmp = digits[0];
            if(tmp >= 10){
                int[] di = new int[digits.length+1];
                int divide = tmp /10;
                int mod = tmp % 10;
                digits[0] = mod ;
                di[0] = divide;
                for(int i = 0; i < digits.length; i++){
                    di[i+1] = digits[i];
                }
                return di;
            }
        
        return digits;
    }

from algorithm007-class02.

Zts0hg avatar Zts0hg commented on July 24, 2024

public int[] plusOne(int[] digits) {
int length = digits.length;
int pos = length - 1;
digits[pos]++;
while(pos > 0){ //最高位涉及增加位数,后面单独处理
// 因为只加1,所以需要进位的情况为:当前位的值为10,则高位数字+1,当前位的值置0
if(digits[pos] >= 10){
int digits[pos] = 0;
int digits[pos-1] ++;
}
length --;
}

        if(digits[0] >= 10){
            int[] di = new int[digits.length+1];
            digits[0] = 0 ; // 次高位
            di[0] = 1; // 最高位
            for(int i = 0; i < digits.length; i++){
                di[i+1] = digits[i];
            }
            return di;
        }
    return digits;
}

没学过java,直接拷贝了@jeff-liu14 的代码,然后进行了一些修改。
思路是,我们已经知道增加的数值为1,所以取余的操作直接变成置0,进位操作直接变成高位加1.

from algorithm007-class02.

zcrazycpp avatar zcrazycpp commented on July 24, 2024

这个题我最开始还陷入了无趣,如果最后一位+1后大于9,就把数组的值转化为int数来+1,然后再转回数组。
这个方法会遇到到测试用例中数组特别大,超出了int的值域,导致提交失败的情况。

from algorithm007-class02.

memorys avatar memorys commented on July 24, 2024

这个问题解决如果一个正整数加一的角度考虑去实现问题,无法绕开的问题是数值的Max和Min值问题。不建议采用。
我把这个问题看成一个逢十进一的问题。数组高位开始检索满足十进一条件将数值赋值为0开始继续检索第二高位,如果不满足条件数值加1返回即可。以此类推代码如下:

public static int[] plusOne(int[] digits) {
    int size = digits.length;
    for (int j = size -1 ; j >= 0; j --) {
        if(digits[j] == 9){
            if(j == 0){
                int[] r = new int[size + 1];
                r[0] = 1;
                return r;
            }
            digits[j] = 0;
        }else{
            digits[j] ++;
            break;
        }
    }
    return digits;
}

from algorithm007-class02.

jazzlly avatar jazzlly commented on July 24, 2024

学习了!

from algorithm007-class02.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.