leetcode66 将数组表示的非负整数加一

题目要求:一个非负整数被表示为一个数组,数组中每一个元素代表该整数的一个位。数组的下标越小,代表的位数越高。现在对该数组做加一运算,请返回结果数组。

/**
 * @author rale
 *
 * Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
 * You may assume the integer do not contain any leading zero, except the number 0 itself.
 * The digits are stored such that the most significant digit is at the head of the list.
 */
public class PlusOne {

    public int[] plusOne(int[] digits) {
        //此处可以直接将carry(进位)设置为1,优化程序
        //carry = 0
        //digits[digits.length-1] += 1 ;
        int carry = 1;
        int temp = 0;
        for(int i=digits.length-1 ; i>=0 ; i--){
            temp = digits[i] + carry;
            digits[i] = temp%10;
            carry = temp/10;
        }
        if(carry>0){
            int[] result = new int[digits.length+1];
            result[0] = 1;
            for(int j = 1 ; j<result.length ; j++){
                result[j] = digits[j-1];
            }
            return result;
        }
        return digits;
    }
}

继续优化
只有当需要进位的时候,加法才需要继续下去,否则加法则可以在当前位停止。
可以在循环中添加判断,若carry==0,则提前跳出循环

/**
 * @author rale
 *
 * Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
 * You may assume the integer do not contain any leading zero, except the number 0 itself.
 * The digits are stored such that the most significant digit is at the head of the list.
 */
public class PlusOne {

    public int[] plusOne(int[] digits) {
        //此处可以直接将carry(进位)设置为1,优化程序
        //carry = 0
        //digits[digits.length-1] += 1 ;
        int carry = 1;
        int temp = 0;
        for(int i=digits.length-1 ; i>=0 ; i--){
            temp = digits[i] + carry;
            digits[i] = temp%10;
            carry = temp/10;
            if(carry==0){
                break
            }
        }
        if(carry>0){
            int[] result = new int[digits.length+1];
            result[0] = 1;
            for(int j = 1 ; j<result.length ; j++){
                result[j] = digits[j-1];
            }
            return result;
        }
        return digits;
    }
}

再再再次优化
此处优化最高位进位的情况
最高位出现进位,当且仅当其他位都产生进位且为0
优化后的代码如下

/**
 * @author rale
 *
 * Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
 * You may assume the integer do not contain any leading zero, except the number 0 itself.
 * The digits are stored such that the most significant digit is at the head of the list.
 */
public class PlusOne {

    public int[] plusOne(int[] digits) {
        int carry = 1;
        int temp = 0;
        for(int i=digits.length-1 ; i>=0 ; i--){
            temp = digits[i] + carry;
            digits[i] = temp%10;
            carry = temp/10;
        }
        if(carry>0){
            int[] result = new int[digits.length+1];
            result[0] = 1;
//          最高位进位的情况只有一种,即其它位均进位且为0,无需再循环一次
//            for(int j = 1 ; j<result.length ; j++){
//                result[j] = digits[j-1];
//            }
            return result;
        }
        return digits;
    }    
}

clipboard.png
想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值