【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 ...
随机推荐
- 分页框架pager-taglib学习笔记
说到分页其实可以研究一下我自己项目里面的分页框架的使用. 下面的笔记来自于孔浩老师的视频教程和我自己的开发实践. 使用Pager-taglib可以帮助我们快速开发分页处理. 下载:pager-tagl ...
- python 数字系列-复数的数学运算
复数的数学运算 问题 你写的最新的网络认证方案代码遇到了一个难题,并且你唯一的解决办法就是使用复数空间. 再或者是你仅仅需要使用复数来执行一些计算操作. 解决方案 复数可以用使用函数 complex( ...
- MySQL - 修改数据库文件物理路径
一共两步: 修改my.ini文件的datadir: 将修改前datadir路径下的文件复制到修改后的datadir路径. 注意: my.ini可能有多个,windows 系统可以在 MySQL 服务的 ...
- Vagrant 手册之 Vagrantfile - 最低版本
原文地址 可以在 Vagrantfile 中指定一组 Vagrant 的版本,以强制人们使用带有 Vagrantfile 的特定版本的 Vagrant.这可以帮助解决使用带有 Vagrantfile ...
- 【ABAP系列】SAP LSWM处理时,网络中断,出现错误
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP LSWM处理时,网络中断 ...
- std::map使用结构体自定义键值
使用STL中的map时候,有时候需要使用结构题自定义键值,比如想统计点的坐标出现的次数 struct Node{ int x,y; }; ...... map<Node,int>mp; m ...
- sql server 2008查询时报错,消息:8155,没有为'a'的列2指向任何列
解决方法1: 关掉Sql Server再打开, 重新查询 解决方法2: select T.* from( select row_number() over(order by age desc) a ...
- openssl使用
一. 加密方法 dsaffdfd fgggg 1.对称加密: 加密算法 + 口令 加密算法: DES(56bits),3DES(用des加密反复加密三次),AES(128bits),Blowfish ...
- response.getWriter()和jsp中的out对象的区别
(1) out和response.getWriter属于的类不同,前者是JspWriter,后者是java.io.PrintWriter.而JspWriter是一个抽象类, PrintWriter是一 ...
- lib.tcl
#********************************************************************# 功能描述:定义公共的函数# 依赖关系:依赖于全局aitoo ...