题目如下:

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.

Note:

  1. 1 <= arr1.length <= 1000
  2. 1 <= arr2.length <= 1000
  3. arr1 and arr2 have no leading zeros
  4. arr1[i] is 0 or 1
  5. arr2[i] is 0 or 1

解题思路:负二进制的计算问题。首先把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的更多相关文章

  1. 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)

    [LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...

  2. 【LeetCode】633. Sum of Square Numbers

    Difficulty: Easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...

  3. 【LeetCode】2、Add Two Numbers

    题目等级:Medium 题目描述:   You are given two non-empty linked lists representing two non-negative integers. ...

  4. 【LeetCode】201. Bitwise AND of Numbers Range

    Bitwise AND of Numbers Range  Given a range [m, n] where 0 <= m <= n <= 2147483647, return ...

  5. 【LeetCode】985. Sum of Even Numbers After Queries 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 找规律 日期 题目地址:https://lee ...

  6. 【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 ...

  7. 【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 ...

  8. 【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 ...

  9. 【LeetCode】633. Sum of Square Numbers 解题报告(python & Java & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 列表生成式 循环 日期 题目地址:https ...

随机推荐

  1. 如何修改linux 用户登录后默认目录

    1.linux用户登录后默认目录是在/etc/passwd文件设置的.如下图所示,一共显示了四行数据,其中第一行的/root即为root用户登录后的默认目录,第二行daemon用户的默认目录是/usr ...

  2. python 浮点运算

    print(format(float(a)/float(b),'.2f'))

  3. UEFI手札

    基于Intel TianoCore衍生的EDK-II诞生的UEFI,用来取代Legacy BIOS. INF文件 Module Information File,模块描述文件.Module可以是可执行 ...

  4. 源码编译apache设置系统启动失败

    文章为转载,亲试成功. Apache无法自动启动,1.将apachectl文件拷贝到/etc/rc.d/init.d 中,然后在/etc/rc.d/rc5.d/下加入链接即可.命令如下:cp /usr ...

  5. MySQL高可用方案 MHA之四 keepalived 半同步复制

    主从架构(开启5.7的增强半同步模式)master: 10.150.20.90   ed3jrdba90slave: 10.150.20.97    ed3jrdba97 10.150.20.132 ...

  6. UI自动化之日志的处理

    写自动化时,我们常常希望打印出浏览器的操作记录,也同时希望报错的记录能够保留并用于问题的排查,这时候可以用到loggging模块 目录 1.logging文件 2.调用日志 1.logging文件 # ...

  7. 类File

    * File类常用的构造方法: * (1)File(String s);//由s确定File对象的文件名 * (2)File(String directory,String s);//由directo ...

  8. 【ABAP系列】SAP GUI740 PATCH5出现弹窗BUG

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP GUI740 PATCH ...

  9. [Web 前端] 019 css 定位之绝对定位与相对定位

    1. 关于定位 我们可以使用 css 的 position 属性来设置元素的定位类型 postion 的设置项如下 设置项 释义 relative 生成相对定位元素元素所占据的文档流的位置不变元素本身 ...

  10. String.indexOf()的使用方法

    String.indexOf()的用途: 返回此字符串中第一个出现的指定的子字符串,如果没有找到则返回-1 源码如下: /** * Returns the index within this stri ...