当前位置:网站首页>leetcode:66. add one-tenth

leetcode:66. add one-tenth

2022-06-11 18:54:00 uncle_ ll

66. Add one

source : Power button (LeetCode)

link : https://leetcode.cn/problems/plus-one/

Given a by Integers Composed of Non empty The nonnegative integer represented by an array , Add one to the number .

The highest digit is placed at the top of the array , Each element in the array stores only a single number .

You can assume that except for integers 0 outside , This integer will not start with zero .

Example 1:

 Input :digits = [1,2,3]
 Output :[1,2,4]
 explain : Input array represents number  123.

Example 2:

 Input :digits = [4,3,2,1]
 Output :[4,3,2,2]
 explain : Input array represents number  4321.

Example 3:

 Input :digits = [0]
 Output :[1]

Tips :

1 <= digits.length <= 100
0 <= digits[i] <= 9

solution

  • The new array holds the calculated elements : Use the new storage space to store the transformed elements of the original array , Notice if the header has a carry ;
  • From the back to the front, find the first not for 9 The elements of : Find the first one not for 9 The elements of , Add to this bit 1, And set the following elements to 0 that will do ; If it's all 9, Then create a new array , The head is 1, The back is full of 0;

Code implementation

New array assignment

python Realization

class Solution:
    def plusOne(self, digits: List[int]) -> List[int]:
        n = len(digits)
        addFlag = 0  #  Carry mark 
        res = []
        count = 0  #  First time of calculation , At the end of +1, The rest is only added to the carry 
        while digits:
            digit = digits.pop()  #  Look for elements from the end 
            if count == 0:
                newDigit = digit + addFlag + 1
                count += 1
            else:
                newDigit = digit + addFlag
            if newDigit >= 10:
                newDigit = newDigit % 10
                addFlag = 1
            else:
                addFlag = 0
            res.insert(0, newDigit)  #  Insert a new element from the header 
        if addFlag == 1:
            res.insert(0, addFlag)
        return res

c++ Realization

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

            if (currentVal >= 10) {
    
                currentVal = currentVal % 10;
                addFlag = 1;
            }
            else
                addFlag = 0;
            
            res.insert(res.begin(), currentVal);
        }
        if (addFlag == 1)
            res.insert(res.begin(), addFlag);
        return res;
    }
};

Complexity analysis

  • Time complexity : O ( n ) O(n) O(n)
  • Spatial complexity : O ( n ) O(n) O(n)

Look for the first non from the back 9 Number of numbers

python Realization

class Solution:
    def plusOne(self, digits: List[int]) -> List[int]:
        n = len(digits)
        for i in range(n-1, -1, -1):
            if digits[i] != 9:
                digits[i] += 1
                for j in range(i+1, n):
                    digits[j] = 0
                return digits
        return [1] + [0] * n

c++ Realization

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

Complexity analysis

  • Time complexity : O ( n ) O(n) O(n)
  • Spatial complexity : O ( 1 ) O(1) O(1) , The new return array is not considered , All for 9 This is still rare
原网站

版权声明
本文为[uncle_ ll]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111853142852.html