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 = 2

Example 2:

Input: 3
Output: "111"
Explantion: (-2) ^ 2 + (-2) ^ 1 + (-2) ^ 0 = 3

Example 3:

Input: 4
Output: "100"
Explantion: (-2) ^ 2 = 4

Note:

  1. 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">

#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<set>
using namespace std;
int main() {
    string str;
    cin >> str;
    map<char, int> mp;
    for (int i = 0; i < str.length(); ++i) {
        mp[str[i]]++;
    }
    int start = 0, cur = 0, end;
    vector<int> ans;
    for (int i = 0; i < str.length(); ++i) {
        if (mp[str[cur]] == 0) {
            bool flag = false;
            for (int j = 1; j < i; ++j) {
                if (mp[str[j]] != 0) {
                    cur = j;
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                ans.push_back(i-start);
                start = i;
                cur = start;
            }
        }
        mp[str[i]]--;
    }
    ans.push_back(str.length()-start);
    if (!ans.empty()) {
        cout << ans[0];
    }
    for (int i = 1; i < ans.size(); ++i)
        cout << " " << ans[i];
    return 0;
}

</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的更多相关文章

  1. 【leetcode】1017. Convert to Base -2

    题目如下: Given a number N, return a string consisting of "0"s and "1"s that represe ...

  2. convert from base 10 to base 2

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION Hence, we convert fro ...

  3. [Swift]LeetCode1017. 负二进制转换 | Convert to Base -2

    Given a number N, return a string consisting of "0"s and "1"s that represents it ...

  4. 进制-Iterative-进制转换

    2019-12-02 21:15:31 进制转换是计算机科学里的一个基础算法,通常可以使用如下的模版来进行计算. 下面我们来讨论一些关于进制的题目. 1271. Hexspeak  问题描述: 问题求 ...

  5. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  6. 转载:C++ 多继承和虚继承的内存布局

    C++ 多继承和虚继承的内存布局[已翻译100%] 英文原文:Memory Layout for Multiple and Virtual Inheritance 标签: <无> run_ ...

  7. PHP7函数大全(4553个函数)

    转载来自: http://www.infocool.net/kb/PHP/201607/168683.html a 函数 说明 abs 绝对值 acos 反余弦 acosh 反双曲余弦 addcsla ...

  8. 前端试题本(Javascript篇)

    JS1. 下面这个JS程序的输出是什么:JS2.下面的JS程序输出是什么:JS3.页面有一个按钮button id为 button1,通过原生的js如何禁用?JS4.页面有一个按钮button id为 ...

  9. Kooboo CMS 之TextContent详解

    TextCotent 在Kooboo.CMS.Content下面,在View中有使用到这个模型层. TextContent继承了ContentBase,而ContentBase是由2个部分类组成的,一 ...

随机推荐

  1. SpringBoot(九):SpringBoot集成Mybatis

    (1)新建一个SpringBoot工程,在pom.xml中配置相关jar依赖 贴代码: <!--加载mybatis整合springboot--> <dependency> &l ...

  2. JS产生GUID

    //生成全球唯一字符串function guidGenerator() { var S4 = function () { return (((1 + Math.random()) * 0x10000) ...

  3. Zeebe服务学习1-简单部署与实现demo

    1.Zeebe是什么? Camunda公司研发的工作流引擎Zeebe,目标是对微服务的编排.具体详细介绍可以参考官网:https://zeebe.io/what-is-zeebe/ 2.背景 随着微服 ...

  4. Linux速通06 系统的初始化服务和监控

    Linux系统引导的顺序 # 掌握 Linux系统引导的顺序 * BIOS的工作是检查计算机的硬件设备,如CPU.内存和风扇速度等 * MBR会在启动盘的第一个块中,大小为512B,其中前446B是引 ...

  5. 2020年12月-第02阶段-前端基础-CSS Day02

    CSS Day02 复合选择器 后代选择器 并集选择器 1. CSS复合选择器 理解 理解css复合选择器分别的应用场景 为什么要学习css复合选择器 CSS选择器分为 基础选择器 和 复合选择器 , ...

  6. Celery:小试牛刀

    Celery是如何工作的? Celery 由于 其分布式体系结构,在某种程度上可能难以理解.下图是典型Django-Celery设置的高级示意图(FROM O'REILLY): 当请求到达时,您可以在 ...

  7. FreeBSD 的xfce 终端动态标题不显示问题解决了:

    tcsh配置,home目录创建.tcshrc, 写入以下配置 alias h history 25 alias j jobs -l alias la ls -aF alias lf ls -FA al ...

  8. VMware 虚拟机安装(使用CentOS镜像)

    (1)启动虚拟机,点击"文件"-->"新建虚拟机",选择安装类型,推荐"典型".       (2)选择稍后安装操作系统       ...

  9. Educational Codeforces Round 64 C. Match Points 【二分思想】

    一 题面 C. Match Points 二 分析 根据题意很容易想到要去找满足条件的数,因为可以打乱输入的顺序,所以很容易想到二分. 但是如果直接对输入的数组进行二分,如输入$a$,直接在数组里二分 ...

  10. 使用docker搭建sonarqube

    sonarqube是一款代码质量检查工具,使用sonar扫描我们写过的代码,可以有助于检查出代码的bug.规范性和健壮性,有助于提高我们的代码质量. 一.安装docker 安装完成之后,命令行输入 d ...