美文网首页LeetCode
[LeetCode] 66. 加一

[LeetCode] 66. 加一

作者: 拉面小鱼丸 | 来源:发表于2018-04-09 14:05 被阅读0次

给定一个非负整数组成的非空数组,给整数加一。

可以假设整数不包含任何前导零,除了数字0本身。

最高位数字存放在列表的首位。

原文

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.

Java

class Solution {
    public int[] plusOne(int[] digits) {
        int temp = 1;
        for (int i = digits.length - 1; i >= 0; i--) {
            if (temp == 0) {
                break;
            }
            if (digits[i] == 9) {
                digits[i] = 0;
            } else {
                digits[i] += temp;
                temp = 0;
            }
        }
        if (temp == 1) {
            int[] res = new int[digits.length + 1];
            res[0] = 1;
            for (int i = 0; i < digits.length; i++) {
                res[i + 1] = digits[i];
            }
            return res;
        }
        return digits;
    }
}

相关文章

  • python实现leetcode之66. 加一

    解题思路 按位加,注意进位 66. 加一[https://leetcode-cn.com/problems/plu...

  • LeetCode:66. 加一

    问题链接 66. 加一[https://leetcode-cn.com/problems/plus-one] 问题...

  • Leetcode-66 加一

    66. 加一[https://leetcode-cn.com/problems/plus-one/] 解题思路 1...

  • 66. 加一

    题目地址(66. 加一) https://leetcode.cn/problems/plus-one/[https...

  • LeetCode(66. 加一)

    算法描述 : 算法实现 : Java实现 :

  • 66. 加一 leetcode

  • [LeetCode] 66. 加一

    给定一个非负整数组成的非空数组,给整数加一。 可以假设整数不包含任何前导零,除了数字0本身。 最高位数字存放在列表...

  • 【Leetcode】66. 加一

    作者: 码蹄疾毕业于哈尔滨工业大学。 小米广告第三代广告引擎的设计者、开发者;负责小米应用商店、日历、开屏广告业务...

  • Leetcode 66. 加一

    给定一个非负整数组成的非空数组,在该数的基础上加一,返回一个新的数组。 最高位数字存放在数组的首位, 数组中每个元...

  • LeetCode 66.加一

    给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。最高位数字存放在数组的首位, 数组中每个元素只存...

网友评论

    本文标题:[LeetCode] 66. 加一

    本文链接:https://www.haomeiwen.com/subject/dihihftx.html