zoukankan      html  css  js  c++  java
  • [LeetCode] 66. Plus One 加一

    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.

    一个非负数字存在一个数组里,高位在前面,给这个数加一。

    解法1:从数组的最后一位开始加1,如果是不是9,就直接加1后返回目前的数就可以了。如果是9,最后一位加1后为0进位为1,下一个数也是9加1后为0进位为1,如果在加的过程中有哪一位不是9,那么就加1后返回当前数。如果一直到最高位都是9,当前位变成了0,然后在前面多加个1。由于数组只能在后面添加数字,所以可以把第一个数变为1,最后加一个0;也可以在建一个数组长度比原来多1的数组,第一个元素为1, 其它元素为0。

    解法2:由于数组添加一位数字要在最后,为了方便操作先把数字逆序,然后用取余、取模操作来算当前位的值和进位,最后在逆序输出返回。

    Java:

    public int[] plusOne(int[] digits) {
            
        int n = digits.length;
        for(int i=n-1; i>=0; i--) {
            if(digits[i] < 9) {
                digits[i]++;
                return digits;
            }
            
            digits[i] = 0;
        }
        
        int[] newNumber = new int [n+1];
        newNumber[0] = 1;
        
        return newNumber;
    }  

    Java:

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

    Java:

    class Solution {
        public int[] plusOne(int[] digits) {
            if (digits.length == 0) return digits;
            int carry = 1;
            for (int i = digits.length - 1; i >= 0; --i) {
                if (carry == 0) return digits;
                int sum = digits[i] + carry;
                digits[i] = sum % 10;
                carry = sum / 10;
            }
            
            if (carry == 0) return digits;
            
            int[] res = new int[digits.length + 1];
            res[0] = 1;
            for (int i=1, i < res.length; i++) {
                rest[i] = digits[i - 1];
            }
            
            return res;
        }
    } 

    Python:

    # in-place solution
    class Solution(object):
        def plusOne(self, digits):
            """
            :type digits: List[int]
            :rtype: List[int]
            """
            for i in reversed(xrange(len(digits))):
                if digits[i] == 9:
                    digits[i] = 0
                else:
                    digits[i] += 1
                    return digits
            digits[0] = 1
            digits.append(0)
            return digits
    

    Python:

    # Time:  O(n)
    # Space: O(n)
    class Solution2(object):
        def plusOne(self, digits):
            """
            :type digits: List[int]
            :rtype: List[int]
            """
            result = digits[::-1]
            carry = 1
            for i in xrange(len(result)):
                result[i] += carry
                carry, result[i] = divmod(result[i], 10)
            if carry:
                result.append(carry)
            return result[::-1]   

    C++: In place, Time: O(n), Space: O(1)

    class Solution {
    public:
        vector<int> plusOne(vector<int>& digits) {
            for (int i = digits.size() - 1; i >= 0; --i) {
                if (digits[i] == 9) {
                    digits[i] = 0;
                } else {
                    ++digits[i];
                    return digits;
                }
            }
            digits[0] = 1;
            digits.emplace_back(0);
            return digits;
        }
    }; 

    C++:  Time: O(n), Space: O(n)

    class Solution2 {
    public:
        vector<int> plusOne(vector<int>& digits) {
            vector<int> result(digits.rbegin(), digits.rend());
            int carry = 1;
            for (auto& num : result) {
                num += carry;
                carry = num / 10;
                num %= 10;
            }
            if (carry == 1) {
                result.emplace_back(carry);
            }
            reverse(result.begin(), result.end());
            return result;  
        }
    };
    

      

    类似题目:
    [LeetCode] 67. Add Binary 二进制数相加

    [LeetCode] 369. Plus One Linked List 链表加一运算

    All LeetCode Questions List 题目汇总

      

  • 相关阅读:
    MODULE_AUTHOR、MODULE_DESCRIPTION、MODULE_LICENSE宏
    Django-RQ首页、文档和下载
    Uncode-Schedule首页、文档和下载
    Kibana+Logstash+Elasticsearch 日志查询系统
    京东集团副总裁李大学:像CEO一样思考
    刘宇(正和磁系资本创始人)_百度百科
    京东离职员工成立“东成西就”微信群 前高管赵国庆李大...
    李大学:互联网裂变里最重要的是资金流|正和岛|正和岛联席总裁黄丽陆|2015正和岛东部(海宁)论坛_新浪财经_新浪网
    nopcommerce中文网
    Getting Started · Building a RESTful Web Service
  • 原文地址:https://www.cnblogs.com/lightwindy/p/8504945.html
Copyright © 2011-2022 走看看