返回介绍

solution / 1000-1099 / 1073.Adding Two Negabinary Numbers / README_EN

发布于 2024-06-17 01:03:31 字数 6028 浏览 0 评论 0 收藏 0

1073. Adding Two Negabinary Numbers

中文文档

Description

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

Solutions

Solution 1

class Solution:
  def addNegabinary(self, arr1: List[int], arr2: List[int]) -> List[int]:
    i, j = len(arr1) - 1, len(arr2) - 1
    c = 0
    ans = []
    while i >= 0 or j >= 0 or c:
      a = 0 if i < 0 else arr1[i]
      b = 0 if j < 0 else arr2[j]
      x = a + b + c
      c = 0
      if x >= 2:
        x -= 2
        c -= 1
      elif x == -1:
        x = 1
        c += 1
      ans.append(x)
      i, j = i - 1, j - 1
    while len(ans) > 1 and ans[-1] == 0:
      ans.pop()
    return ans[::-1]
class Solution {
  public int[] addNegabinary(int[] arr1, int[] arr2) {
    int i = arr1.length - 1, j = arr2.length - 1;
    List<Integer> ans = new ArrayList<>();
    for (int c = 0; i >= 0 || j >= 0 || c != 0; --i, --j) {
      int a = i < 0 ? 0 : arr1[i];
      int b = j < 0 ? 0 : arr2[j];
      int x = a + b + c;
      c = 0;
      if (x >= 2) {
        x -= 2;
        c -= 1;
      } else if (x == -1) {
        x = 1;
        c += 1;
      }
      ans.add(x);
    }
    while (ans.size() > 1 && ans.get(ans.size() - 1) == 0) {
      ans.remove(ans.size() - 1);
    }
    Collections.reverse(ans);
    return ans.stream().mapToInt(x -> x).toArray();
  }
}
class Solution {
public:
  vector<int> addNegabinary(vector<int>& arr1, vector<int>& arr2) {
    int i = arr1.size() - 1, j = arr2.size() - 1;
    vector<int> ans;
    for (int c = 0; i >= 0 || j >= 0 || c; --i, --j) {
      int a = i < 0 ? 0 : arr1[i];
      int b = j < 0 ? 0 : arr2[j];
      int x = a + b + c;
      c = 0;
      if (x >= 2) {
        x -= 2;
        c -= 1;
      } else if (x == -1) {
        x = 1;
        c += 1;
      }
      ans.push_back(x);
    }
    while (ans.size() > 1 && ans.back() == 0) {
      ans.pop_back();
    }
    reverse(ans.begin(), ans.end());
    return ans;
  }
};
func addNegabinary(arr1 []int, arr2 []int) (ans []int) {
  i, j := len(arr1)-1, len(arr2)-1
  for c := 0; i >= 0 || j >= 0 || c != 0; i, j = i-1, j-1 {
    x := c
    if i >= 0 {
      x += arr1[i]
    }
    if j >= 0 {
      x += arr2[j]
    }
    c = 0
    if x >= 2 {
      x -= 2
      c -= 1
    } else if x == -1 {
      x = 1
      c += 1
    }
    ans = append(ans, x)
  }
  for len(ans) > 1 && ans[len(ans)-1] == 0 {
    ans = ans[:len(ans)-1]
  }
  for i, j = 0, len(ans)-1; i < j; i, j = i+1, j-1 {
    ans[i], ans[j] = ans[j], ans[i]
  }
  return ans
}
function addNegabinary(arr1: number[], arr2: number[]): number[] {
  let i = arr1.length - 1,
    j = arr2.length - 1;
  const ans: number[] = [];
  for (let c = 0; i >= 0 || j >= 0 || c; --i, --j) {
    const a = i < 0 ? 0 : arr1[i];
    const b = j < 0 ? 0 : arr2[j];
    let x = a + b + c;
    c = 0;
    if (x >= 2) {
      x -= 2;
      c -= 1;
    } else if (x === -1) {
      x = 1;
      c += 1;
    }
    ans.push(x);
  }
  while (ans.length > 1 && ans[ans.length - 1] === 0) {
    ans.pop();
  }
  return ans.reverse();
}
public class Solution {
  public int[] AddNegabinary(int[] arr1, int[] arr2) {
    int i = arr1.Length - 1, j = arr2.Length - 1;
    List<int> ans = new List<int>();
    for (int c = 0; i >= 0 || j >= 0 || c != 0; --i, --j) {
      int a = i < 0 ? 0 : arr1[i];
      int b = j < 0 ? 0 : arr2[j];
      int x = a + b + c;
      c = 0;
      if (x >= 2) {
        x -= 2;
        c -= 1;
      } else if (x == -1) {
        x = 1;
        c = 1;
      }
      ans.Add(x);
    }
    while (ans.Count > 1 && ans[ans.Count - 1] == 0) {
      ans.RemoveAt(ans.Count - 1);
    }
    ans.Reverse();
    return ans.ToArray();
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文