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. Java基础语法:abstract修饰符

    一.简介 描述: 'abstract'修饰符可以用来修饰方法,也可以修饰类. 如果修饰方法,那么该方法就是抽象方法:如果修饰类,那么该类就是抽象类. 抽象类和抽象方法起到一个框架作用,方便后期扩展的重 ...

  2. Python基础语法函数

    函数是什么 Python中的函数与数学中的函数不同,它不再只是公式,而是实实在在有着自己特定功能的代码.其实在潜移默化中我们已经有所接触了. 比如print()函数,range()函数,type()函 ...

  3. Lua生成Guid(uuid)

    全局唯一标识符(GUID,Globally Unique Identifier)也称作 UUID(Universally Unique IDentifier) .GUID是一种由算法生成的二进制长度为 ...

  4. 解决VM 与 Device/Credential Guard 不兼容(全网有效解决思路)

    为什么要写这篇文章先说背景:前段时间因为学习Linux系统需要,自己本机用的是Windows系统,那这里就需要用到虚拟机来创建虚拟环境用来支持Linux系统 1: 于是乎,自己很激动的下载了vm虚拟机 ...

  5. git的回滚与撤销【reset and revert】

    git的工作流程-- 3个区域 工作区:我们可以看到的文件内容 在操作 git add 之前的!! 缓存区:是不可见的  已经git add操作,还没git commit -m "" ...

  6. Java中的名称命名规范:

    Java中的名称命名规范:(不遵守,也不会出现编译的错误) 包名:多单词组成时所有字母都小写:xxxyyyzzz 类名.接口名:多单词组成时,所有单词的首字母大写:XxxYyyZzz 变量名.方法名: ...

  7. java二叉树遍历——深度优先(DFS)与广度优先(BFS) 递归版与非递归版

    介绍 深度优先遍历:从根节点出发,沿着左子树方向进行纵向遍历,直到找到叶子节点为止.然后回溯到前一个节点,进行右子树节点的遍历,直到遍历完所有可达节点为止. 广度优先遍历:从根节点出发,在横向遍历二叉 ...

  8. WebGPU[1] 三角形

    代码见: https://github.com/onsummer/my-dev-notes/tree/master/webgpu-Notes/01-triangle 如果本篇的代码不能跑了,请联系我或 ...

  9. 接口自动化——读取Excle中遇到的问题

    一.module 'openpyxl' has no attribute 'load_workbook'问题 原因:在pycharm中py文件名字为openpyxl导致 修改方法:重新对py名字进行命 ...

  10. 用DeBug的方式,带你掌握HBase文件在Snapshot的各种变化

    摘要:掌握Snapshot可以帮助我们很好的完成HBase数据备份和数据迁移的工作. 简介 HBase的Snapshot功能可以在不复制数据的情况下,快速克隆一张表,完成一次数据备份.通过Snapsh ...