资讯专栏INFORMATION COLUMN

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

QLQ / 2617人阅读

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

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

/**
 * @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

继续优化
只有当需要进位的时候,加法才需要继续下去,否则加法则可以在当前位停止。
可以在循环中添加判断,若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

再再再次优化
此处优化最高位进位的情况
最高位出现进位,当且仅当其他位都产生进位且为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


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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66834.html

相关文章

  • Leetcode66. 加一

    摘要:作者码蹄疾毕业于哈尔滨工业大学。最高位数字存放在数组的首位,数组中每个元素只存储一个数字。你可以假设除了整数之外,这个整数不会以零开头。示例输入输出解释输入数组表示数字。 作者: 码蹄疾毕业于哈尔滨工业大学。 小米广告第三代广告引擎的设计者、开发者;负责小米应用商店、日历、开屏广告业务线研发;主导小米广告引擎多个模块重构;关注推荐、搜索、广告领域相关知识; 题目 给定一个由整数组成的非...

    Invoker 评论0 收藏0
  • Leetcode66. 加一

    摘要:作者码蹄疾毕业于哈尔滨工业大学。最高位数字存放在数组的首位,数组中每个元素只存储一个数字。你可以假设除了整数之外,这个整数不会以零开头。示例输入输出解释输入数组表示数字。 作者: 码蹄疾毕业于哈尔滨工业大学。 小米广告第三代广告引擎的设计者、开发者;负责小米应用商店、日历、开屏广告业务线研发;主导小米广告引擎多个模块重构;关注推荐、搜索、广告领域相关知识; 题目 给定一个由整数组成的非...

    MorePainMoreGain 评论0 收藏0
  • Leetcode66. 加一

    摘要:作者码蹄疾毕业于哈尔滨工业大学。最高位数字存放在数组的首位,数组中每个元素只存储一个数字。你可以假设除了整数之外,这个整数不会以零开头。示例输入输出解释输入数组表示数字。 作者: 码蹄疾毕业于哈尔滨工业大学。 小米广告第三代广告引擎的设计者、开发者;负责小米应用商店、日历、开屏广告业务线研发;主导小米广告引擎多个模块重构;关注推荐、搜索、广告领域相关知识; 题目 给定一个由整数组成的非...

    hiyang 评论0 收藏0
  • 【刷算法】LeetCode.66-加一

    摘要:题目描述给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。最高位数字存放在数组的首位,数组中每个元素只存储一个数字。你可以假设除了整数之外,这个整数不会以零开头。示例输入输出解释输入数组表示数字。 题目描述 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。 你可以假设除了整数 0 之外,这个...

    苏丹 评论0 收藏0

发表评论

0条评论

QLQ

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<