【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 ...
随机推荐
- elementUI下拉树组件封装
使用组件:Popover 弹出框.Tree 树形控件 和 input 输入框 用法: 1.新建一个.vue文件,粘贴以下组件封装的代码(完全可以使用) 2.在页面需要使用下拉树的地方调用即可. (1) ...
- mysql的my.cnf参数详解
转载[Mysql] MySQL配置文件my.cnf的理解 一.缘由 最近要接手数据库的维护工作,公司首选MySQL.对于MySQL的理解,我认为很多性能优化工作.主从主主复制都是在调整参数,来适应不同 ...
- 查看Oracle数据库中的执行计划
1.set autotrace traceonly命令 2.explain plan for命令 1)explain plan for select * from dual; 2)select * f ...
- Microsoft SQL Server 2008 R2官方中文版(SQL2008下载)
Microsoft SQL Server 2008 R2官方中文版(SQL2008下载) http://www.2cto.com/database/201308/235349.html
- clientdataset 读取excel 如果excel 文件不存在的时候 相应的gird 会不显示数据, 鼠标掠过 gird 格子 才会显示数据。 这是一个bug 哈哈
clientdataset 读取excel 如果excel 文件不存在的时候 相应的gird 会不显示数据, 鼠标掠过 gird 格子 才会显示数据. 这是一个bug 哈哈
- sqoop应用
1.导入数据(将mysql(rdbms)的表的数据导入到hdfs) 1.1.全部导入(注意空格) sqoop import \ --connect jdbc:mysql://192.168.159.1 ...
- python基础数据类型补充以及编码的进阶
一.基本数据类型的补充循环列表改变列表大小的问题#请把列表中索引为基数的元素写出l1=[1,2,3,4,5,6]for i in l1: if i%2!=0: print(i)结果:135二:基本数据 ...
- [19/05/20-星期一] CSS_css的盒子模型
一.盒子模型 <!DOCTYPE html> <html> <!-- div 标签 块级标签 主要用来网页布局, 会将其中的子元素内容作为一个独立的整体存在 默认宽度是页 ...
- Ant-编译构建(2)-第3方jar包引入、log4j2
1.项目目录结构图,lib包引入了一些关于common logging+log4j2相关的jar. 2.编写相关的build.xml <?xml version="1.0" ...
- [BZOJ2588]Count on a tree(LCA+主席树)
题面 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个询问 ...