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. 一文吃透如何部署kubernetes高可用集群

    使用 k8s 官方提供的部署工具 kubeadm 自动安装,需要在 master 和 node 节点上安装 docker 等组件,然后初始化,把管理端的控制服务和 node 上的服务都以 pod 的方 ...

  2. QQ 邀你上线小程序,官方生态能力持续赋能你的小程序

    转: QQ 邀你上线小程序,官方生态能力持续赋能你的小程序 你身边总有一些朋友,他们的表情包极其丰富,能时刻应对各种聊天场景. 表情包奇奇怪怪,可可爱爱,非常形象生动体现我们当下的心情,逐渐成为社交平 ...

  3. ElasticSearch(ES)使用Nested结构存储KV及聚合查询

    自建博客地址:https://www.bytelife.net,欢迎访问! 本文为博客同步发表文章,为了更好的阅读体验,建议您移步至我的博客 本文作者: Jeffrey 本文链接: https://w ...

  4. 聊聊IT技术人的知识体系

    我在我的2020年终总结中提到技术人需要建立自己的知识体系,那么怎么建立自己的知识体系呢?技术人的知识体系又是什么样的呢?今天,和你一一分享. 1 关于我的12字方针 我在我的<2020年终回顾 ...

  5. Fastjson <=1.2.24-反序列化-任意命令执行

    漏洞分析 https://www.secpulse.com/archives/72391.html 复现参考 https://www.cnblogs.com/hack404/p/11980791.ht ...

  6. Java概述与准备

    Java概述 Java语言背景介绍 Java之父:詹姆斯·高斯林(James Gosling)   java语言的三个版本: JavaSE: Java 语言的(标准版),用于桌面应用的开发,是其他两个 ...

  7. WPF 应用 - 通过 js 缩放 System.Windows.Controls.WebBrowser 的内容

    1. 前提 原本是在大屏上展示系统,系统有个功能是加载第三方的网站,第三方网站按照大屏的分辨率写死了宽高: 现需要改到小屏展示系统,而这个第三方的网站不能随着 WebBrowser 窗口的尺寸调整网站 ...

  8. 循环单链表定义初始化及创建(C语言)

    #include <stdio.h> #include <stdlib.h> /** * 含头节点循环单链表定义,初始化 及创建 */ #define OK 1; #defin ...

  9. js 获取树结构的节点深度

    需求:获取树结构的节点深度. 实现util.js: // 获取节点深度 参数为树结构array function getMaxFloor(treeData){ let deep = 0; functi ...

  10. CentOS 8.3安装MySQL 8.0.21后无法登录管理数据库

    安装mysql后登录不了,提示: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) ...