Algorithm leetcode|66. Add one (rust punches hard)



66. Add one:

Given a non-negative integer represented by a non-empty array of integers , add one to the number .

The highest digit is stored at the head of the array, and each element in the array stores only a single number.

You can assume that this integer will not start with a zero other than the integer 0.

Example 1:

输入:
	
	digits = [1,2,3]
	
输出:
	
	[1,2,4]
	
解释:
	
	输入数组表示数字 123。

Example 2:

输入:
	
	digits = [4,3,2,1]
	
输出:
	
	[4,3,2,2]
	
解释:
	
	输入数组表示数字 4321。

Example 3:

输入:
	
	digits = [0]
	
输出:
	
	[1]

hint:

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

analyze:

  • Facing this algorithm problem, the second leader fell into deep thought again.
  • At the beginning, a thought flashed through, just take the last bit of the array, and add one to the end?
  • Although it is a simple question, it should not be taken too seriously.
  • If the last digit is not 9 , it is indeed over, but if it is 9 , a carry will be generated instead of simply adding one.
  • After the carry, the previous digit will be incremented by one, and a carry may occur again, so this question is mainly about dealing with the carry.
  • In the end, it is possible that all the numbers are 9. Carrying from the end to the beginning, there is not enough space. In this case, you can only apply for a new space, and you need to come out with one more.
  • There are three possibilities for analyzing parameters:
    1. There is no 9 at the end , such as [1,2,3], then we directly add one to the number at the end to get [1,2,4] and return.
    2. There are several 9s at the end , such as [1,2,3,9,9], then we only need to find the first element that is not 9 in reverse , that is, 3, add one to this element, and at the same time, all 9s on the way become is 0, get [1,2,4,0,0] and return.
    3. All elements are 9 , such as [9,9,9,9,9], then the answer is [1,0,0,0,0,0]. We only need to construct a new array whose length is 1 more than the parameter length, set the first element to 1, and set the remaining elements to 0.
  • So this problem can be handled in this way, reversely search for the first number that is not 9 from the last digit of the array, add one, and the digits that are 9 on the way become 0, and carry.
  • If all the numbers are 9 , then directly apply for a new space, add one to the length of the source array, and then assign the first digit to 1.

answer:

rust:

impl Solution {
    
    
    pub fn plus_one(mut digits: Vec<i32>) -> Vec<i32> {
    
    
        let n = digits.len();

        for i in (0..n).rev() {
    
    
            if digits[i] == 9 {
    
    
            	// 进位变为0
                digits[i] = 0;
            } else {
    
    
            	// 第一个不是9的位
                digits[i] += 1;
                return digits;
            }
        }

        // digits 中所有的元素均为 9
        let mut ans = vec![0; n + 1];
        ans[0] = 1;
        return ans;
    }
}

go:

func plusOne(digits []int) []int {
    
    
    n := len(digits)

	for i := n - 1; i >= 0; i-- {
    
    
		if digits[i] == 9 {
    
    
			// 进位变为0
			digits[i] = 0
		} else {
    
    
			// 第一个不是9的位
			digits[i]++
			return digits
		}
	}

	// digits 中所有的元素均为 9
	digits = make([]int, n+1)
	digits[0] = 1
	return digits
}

c++:

class Solution {
    
    
public:
    vector<int> plusOne(vector<int>& digits) {
    
    
        const int n = digits.size();

        for (int i = n - 1; i >= 0; --i) {
    
    
            if (digits[i] == 9) {
    
    
                // 进位变为0
                digits[i] = 0;
            } else {
    
    
                // 第一个不是9的位
                ++digits[i];
                return digits;
            }
        }

        // digits 中所有的元素均为 9
        vector<int> ans(n + 1);
        ans[0] = 1;
        return ans;
    }
};

python:

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:
                # 进位变为0
                digits[i] = 0
            else:
                # 第一个不是9的位
                digits[i] += 1
                return digits

        # digits 中所有的元素均为 9
        return [1] + [0] * n


java:

class Solution {
    
    
    public int[] plusOne(int[] digits) {
    
    
        final int n = digits.length;

        for (int i = n - 1; i >= 0; --i) {
    
    
            if (digits[i] == 9) {
    
    
                // 进位变为0
                digits[i] = 0;
            } else {
    
    
                // 第一个不是9的位
                ++digits[i];
                return digits;
            }
        }

        // digits 中所有的元素均为 9
        int[] ans = new int[n + 1];
        ans[0] = 1;
        return ans;
    }
}

Thank you very much for reading this article~
Welcome to [Like][Favorite][Comment] Go three times in a row~It’s
not difficult to give up, but it must be cool~
I hope we all can improve a little every day~
This article is written by the white hat of the second master: https://le-yi.blog.csdn.net/Blog original~


Guess you like

Origin blog.csdn.net/leyi520/article/details/132017913