We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
https://leetcode-cn.com/contest/weekly-contest-139/problems/adding-two-negabinary-numbers/
/** * @param {number[]} arr1 * @param {number[]} arr2 * @return {number[]} */ var addNegabinary = function(arr1, arr2) { arr1.reverse(); arr2.reverse(); if (arr1.length < arr2.length) { arr1.push(...new Array(arr2.length - arr1.length).fill(0)); } else { arr2.push(...new Array(arr1.length - arr2.length).fill(0)); } let result = []; let carry = 0; for (let i = 0; i < arr1.length || carry !== 0; i++) { let n = (arr1[i] || 0) + (arr2[i] || 0) + carry; if (n > 1) { carry = -1; n -= 2; } else if (n === -1) { carry = 1; n = 1; } else { carry = 0; } result.push(n); } result.reverse(); if (result.length > 1 && result[0] === 0) { if (result.every(x => x === 0)) { result.length = 1; } else { let count = 0; for (let i = 0; result[i] === 0; i++) { count++; } result = result.slice(count); } } return result; };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
题目
https://leetcode-cn.com/contest/weekly-contest-139/problems/adding-two-negabinary-numbers/
思路
a. 如果当前位结果大于1, 则进位为-1, 结果 -= 2
b. 如果当前位结果等于-1, 则进位为1, 结果为1
c. 如果是其他情况, 则进位为0, 结果不变
代码
The text was updated successfully, but these errors were encountered: