Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LeetCode] 989. Add to Array-Form of Integer #989

Open
grandyang opened this issue May 30, 2019 · 0 comments
Open

[LeetCode] 989. Add to Array-Form of Integer #989

grandyang opened this issue May 30, 2019 · 0 comments

Comments

@grandyang
Copy link
Owner

grandyang commented May 30, 2019

For a non-negative integer X, the  array-form ofX  is an array of its digits in left to right order.  For example, if X = 1231, then the array form is [1,2,3,1].

Given the array-form A of a non-negative integer X, return the array-form of the integer X+K.

Example 1:

Input: A = [1,2,0,0], K = 34
Output: [1,2,3,4]
Explanation: 1200 + 34 = 1234

Example 2:

Input: A = [2,7,4], K = 181
Output: [4,5,5]
Explanation: 274 + 181 = 455

Example 3:

Input: A = [2,1,5], K = 806
Output: [1,0,2,1]
Explanation: 215 + 806 = 1021

Example 4:

Input: A = [9,9,9,9,9,9,9,9,9,9], K = 1
Output: [1,0,0,0,0,0,0,0,0,0,0]
Explanation: 9999999999 + 1 = 10000000000

Note:

  1. 1 <= A.length <= 10000
  2. 0 <= A[i] <= 9
  3. 0 <= K <= 10000
  4. If A.length > 1, then A[0] != 0

这道题给了一个数组A,说是可以表示一个正整数,高位在开头,又给了一个正数K,让用数组A表示的正整数加上K,并还用数组来表示相加的后的结果。这种用不同的数据结构玩加法的题,之前也出现过,比如 Add Two NumbersPlus OneAdd Binary,和 Add Strings。但其实都是万变不离其宗,都是要一位一位的相加,并且处理好进位的问题。这里由于高位是在数组开头,而相加是要从低位开始的,所以从数组的后面往前开始遍历,用当前位上的数字加上K,再对 10 取余,得到的就是相加后该位上的数字。大家可能会担心进位的问题,进位后的结果会被加到K中,不会丢失,此时K再加上了 A[i] 之后也应该除以 10,然后继续进行循环。当数组A遍历完后,K可能还大于0,此时的K值就应该以数组的形式加到前头,每次将对 10 取余的值加到结果 res 开头,然后K自除以 10 即可,参见代码如下:

解法一:

class Solution {
public:
    vector<int> addToArrayForm(vector<int>& A, int K) {
        vector<int> res;
        for (int i = (int)A.size() - 1; i >= 0; --i) {
            res.insert(res.begin(), (A[i] + K) % 10);
            K = (A[i] + K) / 10;
        }
        while (K > 0) {
            res.insert(res.begin(), K % 10);
            K /= 10;
        }
        return res;
    }
};

当然我们也可以只用一个循环搞定,并且直接更新数组A,不额外使用数组。那么循环条件就是K大于0,或进位值 carry 大于0。在循环中,先让 carry 加上K对 10 取余的值,此时若数组A的值还没有遍历完,即i大于等于0时,再加上 A[i],此时 num 除以 10 就是更新后的 carry 值,对 10 取余就是当前位上的值。此时若i大于等于0,则更新 A[i] 为 num 值,且i自减1,否则将 num 加到数组A的开头,然后K在自除以 10,最后返回数组A即可,参见代码如下:

解法二:

class Solution {
public:
    vector<int> addToArrayForm(vector<int>& A, int K) {
		int n = A.size(), i = n - 1, carry = 0;
		while (K > 0 || carry > 0) {
			int num = K % 10 + carry;
			if (i >= 0) num += A[i];
			carry = num / 10;
			num %= 10;
			if (i >= 0) {
				A[i] = num;
				--i;
			} else {
				A.insert(A.begin(), num);
			}
			K /= 10;
		}
		return A;
    }
};

Github 同步地址:

#989

类似题目:

Add Two Numbers

Plus One

Add Binary

Add Strings

参考资料:

https://leetcode.com/problems/add-to-array-form-of-integer/

https://leetcode.com/problems/add-to-array-form-of-integer/discuss/234488/JavaC%2B%2BPython-Take-K-itself-as-a-Carry

https://leetcode.com/problems/add-to-array-form-of-integer/discuss/234558/JavaPython-3-6-liner-w-comment-and-analysis

LeetCode All in One 题目讲解汇总(持续更新中...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant