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. 【ZeyFraのJavaEE开发小知识01】@DateTimeFomat和@JsonFormat

    @DateTimeFormat 所在包:org.springframework.format.annotation.DateTimeFormat springframework的注解,一般用来对Dat ...

  2. ValidationUtils 验证工具

    package com.appnirman.vaidationutils;import android.content.Context;import java.util.regex.Matcher;i ...

  3. 使用 xunit 编写测试代码

    使用 xunit 编写测试代码 Intro xunit 是 .NET 里使用非常广泛的一个测试框架,有很多测试项目都是在使用 xunit 作为测试框架,不仅仅有很多开源项目在使用,很多微软的项目也在使 ...

  4. 后端程序员之路 59、go uiprogress

    gosuri/uiprogress: A go library to render progress bars in terminal applicationshttps://github.com/g ...

  5. Lua C++交互 应用实例步骤(UserData使用)

    一.配置Lua C++交互环境 1.下载Lua 包环境 地址: https://www.lua.org/download.html ,我们这里用的是5.4.2版本. 2.新建C++ 控制台应用程序 3 ...

  6. linux 查询登陆成功、失败的用户

    查询登陆成功的用户: last 单独执行last指令时,它会读取位于/var/log/wtmp的文件,并把该给文件的内容记录的登录系统的用户名单全部显示出来. 如果使用tail.cat命令查看这文件, ...

  7. 微信小程序和H5之间相互跳转

    1.微信小程序跳转小程序 wx.navigateToMiniProgram <script src='https://res.wx.qq.com/open/js/jweixin-1.3.0.js ...

  8. CCF(再卖菜60分)爆搜+记忆化搜索+差分约束

    201809-4 再卖菜 我使用的是爆搜解决,只得了60分. 记忆化搜索 差分约束 #include<iostream> #include<cstdio> #include&l ...

  9. javascript处理HTML的Encode(转码)和解码(Decode)

    HTML的Encode(转码)和解码(Decode)在平时的开发中也是经常要处理的,在这里总结了使用javascript处理HTML的Encode(转码)和解码(Decode)的常用方式 一.用浏览器 ...

  10. Java 多线程 02

    多线程·线程间通信 和 GUI 单例设计模式 * A:单例设计模式 * 保证类在内存中只有一个对象 * B:如何保证 * a:控制类的创建,不让其他类来创建泵类的对象,私有化构造方法 * b:在本类中 ...