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 = 4
Note:
0 <= N <= 10^9
Approach #1: Math. [Java]
class Solution {
public String baseNeg2(int N) {
if (N == 0) return "0";
StringBuilder sb = new StringBuilder();
while (N != 0) {
int remainder = N % (-2);
N /= -2;
if (remainder < 0) {
remainder += 2;
N += 1;
}
sb.append(remainder);
}
return sb.reverse().toString();
}
}
<pre><code class="html">
</code></pre>
Reference:
https://en.wikipedia.org/wiki/Negative_base#Calculation
https://www.geeksforgeeks.org/convert-number-negative-base-representation/
1017. Convert to Base -2的更多相关文章
- 【leetcode】1017. Convert to Base -2
题目如下: Given a number N, return a string consisting of "0"s and "1"s that represe ...
- convert from base 10 to base 2
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION Hence, we convert fro ...
- [Swift]LeetCode1017. 负二进制转换 | Convert to Base -2
Given a number N, return a string consisting of "0"s and "1"s that represents it ...
- 进制-Iterative-进制转换
2019-12-02 21:15:31 进制转换是计算机科学里的一个基础算法,通常可以使用如下的模版来进行计算. 下面我们来讨论一些关于进制的题目. 1271. Hexspeak 问题描述: 问题求 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- 转载:C++ 多继承和虚继承的内存布局
C++ 多继承和虚继承的内存布局[已翻译100%] 英文原文:Memory Layout for Multiple and Virtual Inheritance 标签: <无> run_ ...
- PHP7函数大全(4553个函数)
转载来自: http://www.infocool.net/kb/PHP/201607/168683.html a 函数 说明 abs 绝对值 acos 反余弦 acosh 反双曲余弦 addcsla ...
- 前端试题本(Javascript篇)
JS1. 下面这个JS程序的输出是什么:JS2.下面的JS程序输出是什么:JS3.页面有一个按钮button id为 button1,通过原生的js如何禁用?JS4.页面有一个按钮button id为 ...
- Kooboo CMS 之TextContent详解
TextCotent 在Kooboo.CMS.Content下面,在View中有使用到这个模型层. TextContent继承了ContentBase,而ContentBase是由2个部分类组成的,一 ...
随机推荐
- 基于SaaS平台的iHRM项目的前端项目介绍
1.下载安装node.js 访问https://nodejs.org/en/,然后下载安装即可 2. 查看是否安装成功 打开cmd命令行,输入node -v 如果出现对应的版本号,即为安装成功 3.从 ...
- c++类的简介
一 类的定义 在c++中,我们使用"类"来描述"对象",可以说"类"的实现是c++OOP的核心. 类中包括对象的"属性" ...
- mysql 单表下的字段操作
如下只介绍单表的添加.更新.删除.查询表结构操作,查询数据操作范围太大用单独的篇幅来讲解: 查看表结构 desc test_tb; Insert 插入数据 插入 = 添加 为表中指定的字段插入数据 C ...
- 二叉树、平衡二叉树、红黑树、B树、B+树与B*树
转: 二叉树.平衡二叉树.红黑树.B树.B+树与B*树 一.二叉树 1️⃣二叉查找树的特点就是左子树的节点值比父亲节点小,而右子树的节点值比父亲节点大,如图: 基于二叉查找树的这种特点,在查找某个节点 ...
- ss_port_change - 一键修改ss配置与Centos7的Firewall策略脚本
ss_port_change 修改ss配置与Centos7的Firewall策略脚本 注意是否需要修改config路径与ss服务的名 脚本的敏感字用了*代替 项目地址 Github 脚本 #!/bin ...
- 178. 分数排名 + MySql + RANK() OVER
178. 分数排名 LeetCode_MySql_178 题目描述 题解分析 排名函数 DENSE_RANK().如果使用 DENSE_RANK() 进行排名会得到:1,1,2,3,4. RANK() ...
- Prometheus + Spring Boot 应用监控
1. Prometheus是什么 Prometheus是一个具有活跃生态系统的开源系统监控和告警工具包.一言以蔽之,它是一套开源监控解决方案. Prometheus主要特性: 多维数据模型,其中包含 ...
- FreeBSD 家图谱
https://cgit.freebsd.org/src/tree/share/misc/bsd-family-tree
- MySQL入门(2)——存储引擎
MySQL入门(2)--存储引擎 查询MySQL支持的存储引擎 查询全部支持的引擎: show engines; ";"可以使用"\g"等价替换,而使用&quo ...
- 2019 GDUT Rating Contest I : Problem A. The Bucket List
题面: A. The Bucket List Input file: standard input Output file: standard output Time limit: 1 second Me ...