【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 ...
随机推荐
- [hadoop](2) MapReducer:Distributed Cache
前言 本章主要内容是讲述hadoop的分布式缓存的使用,通过分布式缓存可以将一些需要共享的数据在各个集群中共享. 准备工作 数据集:ufo-60000条记录,这个数据集有一系列包含下列字段的UFO目击 ...
- <R语言编程艺术>的一个错误以及矩阵相加
R语言编程艺术讲矩阵这节时,举了个随机噪声模糊罗斯福总统画像的例子.但是里面似乎有个错误,例子本意是区域外的值保持不变,而选定区域的值加一个随机值,但是实际情况是两个行列不相等的矩阵相加,会报错,如果 ...
- iOS逆向一个APP指令集
一.脱壳获取.app文件 1.查询壳有没加密 otool -l mac-o文件 | grep crypt 2.Clutch -i Clutch -d num 3.脱壳后的位置 /private/v ...
- Vagrant 手册之同步目录 - VirtualBox
原文地址 如果你使用的 provider 是 VirtualBox,那么 VirtualBox 同步目录就是默认的同步目录类型.这些同步目录使用 VirtualBox 的共享目录系统来同步客户机跟宿主 ...
- Git005--工作区和暂存区
Git--工作区和暂存区 本文来自于:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b0 ...
- web开发jsp页面遇到的一系列问题
一:web总结 1.jsp页面知识点巩固 1.1字符串数字格式化转换 <%@ taglib prefix="fmt" uri="http://java.sun.co ...
- dp(最长升序列)
http://poj.org/problem?id=2533 题意:给你n(1-1000)个数,求这n个数的最长升序列. 题解:dp[i]表示以第i个数结尾的最长升序列. #include & ...
- AC自动机题单
AC自动机题目 真的超级感谢xzy 真的帮到我很多 题单 [X] [luogu3808][模板]AC自动机(简单版) https://www.luogu.org/problemnew/show/P38 ...
- Day3---Python的time库的一些简单函数以及用法
time库的一些函数 time.time () : 获取当前时间戳,即计算机内部时间值,浮点数 >>>import time >>> time.time() 1 ...
- python学习第十九天三元运算符与php语言区别
三元运算符是条件语句的简写,常见的条件语句写三行,三元运算符只需要写一行,python三元运算符是怎么写的呢 1,常见条件判断 if a<b: print(a) else: print(b) 2 ...