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] 1073. Adding Two Negabinary Numbers #1073

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

[LeetCode] 1073. Adding Two Negabinary Numbers #1073

grandyang opened this issue May 30, 2019 · 0 comments

Comments

@grandyang
Copy link
Owner

grandyang commented May 30, 2019

Given two numbers arr1 and arr2 in base -2, return the result of adding them together.

Each number is given in  array format :  as an array of 0s and 1s, from most significant bit to least significant bit.  For example, arr = [1,1,0,1] represents the number (-2)^3 + (-2)^2 + (-2)^0 = -3.  A number arr in  array, format  is also guaranteed to have no leading zeros: either arr == [0] or arr[0] == 1.

Return the result of adding arr1 and arr2 in the same format: as an array of 0s and 1s with no leading zeros.

Example 1:

Input: arr1 = [1,1,1,1,1], arr2 = [1,0,1]
Output: [1,0,0,0,0]
Explanation: arr1 represents 11, arr2 represents 5, the output represents 16.

Example 2:

Input: arr1 = [0], arr2 = [0]
Output: [0]

Example 3:

Input: arr1 = [0], arr2 = [1]
Output: [1]

Constraints:

  • 1 <= arr1.length, arr2.length <= 1000
  • arr1[i] and arr2[i] are 0 or 1
  • arr1 and arr2 have no leading zeros

这道题说是有两个负二进制数是用数组来表示的,现在让返回它们相加后的结果,还是放在数组中来表示。之前其实也遇到过负二进制的题 Convert to Base -2,所以博主自然而然想到的解法就是将其均转为十进制数,然后相加,再将其转为负二进制数。但是这种方法可能会整型溢出,因为给定的数组可能很长,从而表示的负二进制的数转为十进制后可能会越界,这也是之前那道 Add Binary 用字符串表示的二进制进行相加时,不直接转为十进制计算的原因。这道题其实利用的方法跟那道很像,都是一位一位的处理的,直接加到结果 res 数组中的。这里使用两个指针i和j,分别指向数组 arr1 和 arr2 的末尾,然后用个变量 carry 表示进位,当i大于等于0时,carry 加上i指向的数字,并且i自减1,同理,当j大于等于0时,carry 加上j指向的数字,并且j自减1。由于数组中当每位上只能放一个数字,所以让 carry ‘与’上1,并加入到结果 res 数组后。然后需要再填充更高一位上的数字,对于二进制来说,直接右移1位即可,这里由于是负二进制,所以右移1位之后再取负。之后要移除所有的 leading zeros,因为这里高位是加到了 res 的后面,所以要去除末尾的零,使用个 while 去除。最后别忘了将 res 翻转一下返回即可,参见代码如下:

class Solution {
public:
    vector<int> addNegabinary(vector<int>& arr1, vector<int>& arr2) {
        vector<int> res;
        int carry = 0, i = (int)arr1.size() - 1, j = (int)arr2.size() - 1;
        while (i >= 0 || j >= 0 || carry) {
            if (i >= 0) carry += arr1[i--];
            if (j >= 0) carry += arr2[j--];
            res.push_back(carry & 1);
            carry = -(carry >> 1);
        }
        while (res.size() > 1 && res.back() == 0) res.pop_back();
        reverse(res.begin(), res.end());
        return res;
    }
};

Github 同步地址:

#1073

类似题目:

Add Binary

Convert to Base -2

参考资料:

https://leetcode.com/problems/adding-two-negabinary-numbers/

https://leetcode.com/problems/adding-two-negabinary-numbers/discuss/303795/C%2B%2B-From-Wikipedia

https://leetcode.com/problems/adding-two-negabinary-numbers/discuss/303751/C%2B%2BPython-Convert-from-Base-2-Addition

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

@grandyang grandyang changed the title [LeetCode] 1073. Missing Problem [LeetCode] 1073. Adding Two Negabinary Numbers Mar 28, 2021
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