【leetcode】1073. Adding Two Negabinary Numbers
题目如下:
Given two numbers
arr1andarr2in 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 numberarrin array format is also guaranteed to have no leading zeros: eitherarr == [0]orarr[0] == 1.Return the result of adding
arr1andarr2in 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.Note:
1 <= arr1.length <= 10001 <= arr2.length <= 1000arr1andarr2have no leading zerosarr1[i]is0or1arr2[i]is0or1
解题思路:负二进制的计算问题。首先把arr1和arr2中较短的那个前面补零直到两者长度一样,由于计算会有进位的情况,因为是负进制,最多只能进两位,所以还需要在arr1和arr2前面补两个零。假设用carry表示进位的情况,那么显然carry的取值只能是0,-1,1。0表示不需要进位,-1表示进位的值和当前位数的值的符号不一致,而1表示1致。例如11 + 01,末位的两个1相加需要进位到倒数第二位,由于倒数第二位的符号和末位是不一致的,因此记carry=-1;又01 + 01,末位进位到倒数第三位,两者符号一致,所以carry为1。记v = arr1[i] + arr2[i],显然v只能是0,1,2三种取值,加上carry的取值,很轻松的可以得出如下关系:

代码如下:
class Solution(object):
def addNegabinary(self, arr1, arr2):
"""
:type arr1: List[int]
:type arr2: List[int]
:rtype: List[int]
"""
length = max(len(arr1), len(arr2))
arr1 = [0]*2 + [0] * (length - len(arr1)) + arr1
arr2 = [0]*2 + [0] * (length - len(arr2)) + arr2
res = []
carry = 0
for i in range(len(arr1)):
inx = len(arr1) - i - 1
v = arr1[inx] + arr2[inx]
#carry: 0,1,-1 ; v:0,1,2
if carry == 1 and v == 0:
res = [1] + res
carry = 0
elif carry == 1 and v == 1:
res = [0] + res
carry = -1
elif carry == 1 and v == 2:
res = [1] + res
carry = -1
elif carry == -1 and v == 0:
res = [1] + res
carry = 1
elif carry == -1 and v == 1:
res = [0] + res
carry = 0
elif carry == -1 and v == 2:
res = [1] + res
carry = 0
elif carry == 0 and v <= 1:
res = [v] + res
elif carry == 0 and v == 2:
res = [0] + res
carry = -1 while len(res) > 1:
if res[0] == 0:
res.pop(0)
continue
break return res
【leetcode】1073. Adding Two Negabinary Numbers的更多相关文章
- 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)
[LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...
- 【LeetCode】633. Sum of Square Numbers
Difficulty: Easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...
- 【LeetCode】2、Add Two Numbers
题目等级:Medium 题目描述: You are given two non-empty linked lists representing two non-negative integers. ...
- 【LeetCode】201. Bitwise AND of Numbers Range
Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return ...
- 【LeetCode】985. Sum of Even Numbers After Queries 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 找规律 日期 题目地址:https://lee ...
- 【leetcode】Sum Root to Leaf Numbers(hard)
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- 【LeetCode】Sum Root to Leaf Numbers
题目 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a num ...
- 【leetcode】985. Sum of Even Numbers After Queries
题目如下: We have an array A of integers, and an array queries of queries. For the i-th query val = quer ...
- 【LeetCode】633. Sum of Square Numbers 解题报告(python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 列表生成式 循环 日期 题目地址:https ...
随机推荐
- el-date-picker用法
需求:1.默认时间是当天开始到此刻的时间 2.快捷键为今天.昨天.最近一周.最近30天.最近90天 3.不可以清空,必选项
- 十、补充数据类型set
set:无顺序的不重复的集合 list---允许重复,修改tuple--允许重复,不能修改 set----不允许重复的集合例子: s=set()print s l1=[11,22,33,22,11]l ...
- RAM: Residual Attention Module for Single Image Super-Resolution
1. 摘要 注意力机制是深度神经网络的一个设计趋势,其在各种计算机视觉任务中都表现突出.但是,应用到图像超分辨领域的注意力模型大都没有考虑超分辨和其它高层计算机视觉问题的天然不同. 作者提出了一个新的 ...
- MySQL使用Navicat远程连接时报错1251
1.报错信息 client does not support authentication protocol requested by server:consider upgrading MySQL ...
- <每日一题> Day2:CodeForces-1141C.PolycarpRestoresPermutation(思维题)
原题链接 参考代码: #include <iostream> #include <cstring> using namespace std; + , INF = 0x3f3f3 ...
- 【洛谷p1190】接水问题
接水问题[题目链接] 咱也不知道为啥咱就是想写博客emmm 尽管这个题是2010年的普及组但是我太菜了并不会写emm 其实感觉这道题是纯纯正正的大模拟: 算法标签中的贪心是没有意义的啊?? SOLUT ...
- 洛谷 P3182 [HAOI2016]放棋子(高精度,错排问题)
传送门 解题思路 不会错排问题的请移步——错排问题 && 洛谷 P1595 信封问题 这一道题其实就是求对于每一行的每一个棋子都放在没有障碍的地方的方案数. 因为障碍是每行.每列只有一 ...
- BZOJ 1875(DP+矩阵快速幂)
题面 传送门 分析 容易想到根据点来dp,设dp[i][j]表示到i点路径长度为j的方案数 状态转移方程为dp[i][k]=∑(i,j)∈Edp[j][k−1]" role="pr ...
- 修复线上bug
1,git branch new_branch 2,git push origin new_branch 以上是线上地址操作,以下是本地仓库操作 3,git fetch 4,git checkout ...
- 反射、getattr
#coding=utf-8 class Dog(object): def __init__(self,name): self.name = name def eat(self): print '123 ...