【leetcode】1017. Convert to Base -2
题目如下:
Given a number
N
, return a string consisting of"0"
s and"1"
s that represents its value in base-2
(negative two).The returned string must have no leading zeroes, unless the string is
"0"
.Example 1:
Input: 2
Output: "110"
Explantion: (-2) ^ 2 + (-2) ^ 1 = 2Example 2:
Input: 3
Output: "111"
Explantion: (-2) ^ 2 + (-2) ^ 1 + (-2) ^ 0 = 3Example 3:
Input: 4
Output: "100"
Explantion: (-2) ^ 2 = 4Note:
0 <= N <= 10^9
解题思路:以12为例,转成二进制后是1100,假设最右边的下标为0,从右往左下标一次加1。很显然,下标是偶数位的情况,值是正数;而下标为奇数位的情况值是负数。为了要弥补负数带来的影响,需要加上等于这个负数*2的绝对值,而这个值恰好就是该负数的下一位。因此只要奇数位出现了1,那么相应就要在其后面的偶数位加上1,如果偶数位本来就是1,则需要考虑进位。
代码如下:
class Solution(object):
def baseNeg2(self, N):
"""
:type N: int
:rtype: str
"""
bs = bin(N)[2:][::-1]
res = [int(i) for i in list(bs)] def carrier(res,i):
if i == len(res) - 1:
res += [1]
if len(res) % 2 == 0:
res += [1]
else:
res[i + 1] += 1
for i in range(1,len(res)):
if res[i] == 0:
continue
elif res[i] == 1 and i % 2 == 1:
carrier(res,i)
elif res[i] == 2:
res[i] = 0
carrier(res, i)
return ''.join([str(i) for i in res[::-1]])
【leetcode】1017. Convert to Base -2的更多相关文章
- 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)
[LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【LeetCode】405. Convert a Number to Hexadecimal 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】108. Convert Sorted Array to Binary Search Tree 解题报告 (Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】538. Convert BST to Greater Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【LeetCode】108. Convert Sorted Array to Binary Search Tree
Problem: Given an array where elements are sorted in ascending order, convert it to a height balance ...
- 【Leetcode】109. Convert Sorted List to Binary Search Tree
Question: Given a singly linked list where elements are sorted in ascending order, convert it to a h ...
- 【leetcode】1290. Convert Binary Number in a Linked List to Integer
题目如下: Given head which is a reference node to a singly-linked list. The value of each node in the li ...
- 【leetcode】538. Convert BST to Greater Tree
题目如下: Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the orig ...
- 【LeetCode】426. Convert Binary Search Tree to Sorted Doubly Linked List 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
随机推荐
- mysql中 key 、primary key 、unique key 和 index 有什么不同
mysql中 key .primary key .unique key 和 index 有什么不同 key 是数据库的物理结构,它包含两层意义和作用, 一是约束(偏重于约束和规范数据库的结构完整性), ...
- ppium使用方法说明
global driver# 元素定位driver.find_element_by_id("id") # id定位driver.find_element_by_name(" ...
- python中生成器generator
通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素,那后面绝大多数元素 ...
- mui初级入门教程(二)— html5+ webview 底部栏用法详解
文章来源:小青年原创发布时间:2016-05-19关键词:mui,html5+,webview转载需标注本文原始地址: http://zhaomenghuan.github.io/#!/blog/20 ...
- 跨域、this指向
不要再问我跨域的问题了: https://segmentfault.com/a/1190000015597029?utm_source=tag-newest 不要再问我this的指向问题了: http ...
- EZOJ #385 排列
分析 对于第一问我们直接从上到下枚举所有横边 每一次交换两边的列标号即可 对于第二问我们发现答案就是最终序列的逆序对数量 代码 #include<bits/stdc++.h> using ...
- 【Unity 系统知识】 Time类
[转载请注明出处] //表示时间总量Time.time:(只读)表示从程序运行的总时间,会随着游戏的暂停而停止计算.Time.unscaledTime:(只读)不考虑timescale对时间修改的总时 ...
- VS代码自动补全功能
VS代码自动补全功能 新建工程后,依次打开 工具>>代码段管理器>>选择C++>>点击 添加(A)...按钮 ,设置你的代码块的目录 复制以下代码并存为note.s ...
- JS验证数字
//1.验证数字 var reg = new RegExp("^[0-9]*$"); //var reg = /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+ ...
- Orcle获取当前时间加小时
如下是oracle 获取当前数据库时间加2个小时 select to_date(TO_CHAR (SYSDATE, 'yyyy-mm-dd hh24:mi:ss'),'yyyy-mm-dd hh24: ...