题目如下:

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. rime 同文输入法 安卓系统

    下载安装APP 从google play下载同文输入法 默认安装完只有3个输入法, 没有五笔和拼音 [官网][https://rime.im/download/] 获取五笔拼音方案 然后找到 /sdc ...

  2. iOS即时通讯之CocoaAsyncSocket源码解析四

    原文 前言: 本文为CocoaAsyncSocket源码系列中第二篇:Read篇,将重点涉及该框架是如何利用缓冲区对数据进行读取.以及各种情况下的数据包处理,其中还包括普通的.和基于TLS的不同读取操 ...

  3. linux 下spyder安装

    linux  下spyder安装: 安装qt4,python3 对应qt5 sudo apt-get install libxext6 libxext-dev libqt4-dev libqt4-gu ...

  4. Java接口继承

    Java中接口可以继承接口,并且可以多继承.

  5. lua-resty-kafka erro xxxx could not be resolved (3: Host not found)

    问题:使用 lua-resty-kafka 向 kafka 发送数据失败,报错如下: slave6 could not be resolved (: Host not found) 配置信息: lua ...

  6. String 和 new String()的区别

    String 和 new String()的区别 For Example String str1 = "ABC" String str2 = new String("AB ...

  7. Struts2异常:HTTP Status 404 - There is no Action mapped for action name addBook.

    HTTP Status 404 - There is no Action mapped for action name addBook. 在地址栏进行访问的时候,出现了这个错误信息,导致出现此异常的原 ...

  8. (136)leetcode刷题Python笔记——只出现一次的数字

    题目如下: 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. 你可以不使用额外空间来实现吗? 示例 1: ...

  9. <<Java RESTful Web Service实战>> 读书笔记

    <<Java RESTful Web Service实战>> 读书笔记 第一章   JAX-RS2.0入门 REST (Representational State ransf ...

  10. Trapping Rain Water I && II

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...