原题链接在这里:https://leetcode.com/problems/base-7/#/description

题目:

Given an integer, return its base 7 string representation.

Example 1:

Input: 100
Output: "202"

Example 2:

Input: -7
Output: "-10"

Note: The input will be in range of [-1e7, 1e7].

题解:

每次num%7放在前面. num再除以7. 最后加上正负号.

注意corner case: num = 0.

Time Complexity: O(num/7). Space: O(1).

AC Java:

 public class Solution {
public String convertToBase7(int num) {
if(num == 0){
return "0";
} boolean isNeg = false;
if(num < 0){
isNeg = true;
} num = Math.abs(num);
StringBuilder sb = new StringBuilder();
while(num != 0){
sb.insert(0, num%7);
num /= 7;
} return isNeg ? "-"+sb.toString() : sb.toString();
}
}

LeetCode Base 7的更多相关文章

  1. [LeetCode] Base 7 基数七

    Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "202&q ...

  2. [LeetCode] Smallest Good Base 最小的好基数

    For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. Now given a str ...

  3. [LeetCode] 483. Smallest Good Base 最小的好基数

    For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. Now given a str ...

  4. [LeetCode] 504. Base 7 基数七

    Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "202&q ...

  5. 【LeetCode】504. Base 7 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 内建库 BigInteger类 逐位计算 倍数相加 ...

  6. 45. leetcode 504. Base 7

    504. Base 7 Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: ...

  7. LeetCode算法题-Base 7(Java实现)

    这是悦乐书的第247次更新,第260篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第114题(顺位题号是504).给定一个整数,返回其基数为7的字符串表示.例如: 输入: ...

  8. #Leetcode# 1009. Complement of Base 10 Integer

    https://leetcode.com/problems/complement-of-base-10-integer/ Every non-negative integer N has a bina ...

  9. [LeetCode&Python] Problem 504. Base 7

    Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "202&q ...

随机推荐

  1. dict字典常用方法总结,数据解构(解包)

    dict {'name':'holle'}字典存储大量关联型数据,可迭代的,最多只有200个键.查询数据速度非常快,符合二分查找(有100个数比如找75会先找到50然后判断,所以2^7次方7次即可找到 ...

  2. PAT 天梯赛 L1-037. A除以B 【水】

    题目链接 https://www.patest.cn/contests/gplt/L1-037 AC代码 #include <iostream> #include <cstdio&g ...

  3. 配置树莓派3和局域网NTP服务器实现内网时间校准

    一.配置局域网NTP服务器 1.安装ntp-4.2.8p5-win32-setup.exe 下载地址:https://www.meinbergglobal.com/english/sw/ntp.htm ...

  4. Android开发BUG及解决方法1

    错误描述: 问题1: Error:Execution failed for task ':app:transformClassesWithDexForDebug'. > com.Android. ...

  5. HTML学习笔记(上)

    1. HTML介绍 1.1 什么是HTML HyperText Markup Language,超文本标记语言.简单来说,HTML文件本质上就是一个文本文件,但是这个文本文件是带有标签的. 不同的标签 ...

  6. 12个提问频率最高的php面试题

    你是否正在准备寻找一份PHP开发的工作,并且也在寻找一些关于PHP的面试题及答案?本文为大家分享了一些被提问频率最高的11个PHP面试题,以及对应的常规回答,每个公司都有自己的面试标准,面试和问题是完 ...

  7. Kubernetes Rook

    Rook Rook 是一个开源的cloud-native storage编排, 提供平台和框架:为各种存储解决方案提供平台.框架和支持,以便与云原生环境本地集成. Rook 将存储软件转变为自我管理. ...

  8. Oracle常用知识小总结

    永不放弃,一切皆有可能!!! 只为成功找方法,不为失败找借口! Oracle常用知识小总结 1. 创建自增主键 对于习惯了SQL SERVER的图形化界面操作的SQLer,很长一段时间不用oracle ...

  9. DL四(预处理:主成分分析与白化 Preprocessing PCA and Whitening )

    预处理:主成分分析与白化 Preprocessing:PCA and Whitening 一主成分分析 PCA 1.1 基本术语 主成分分析 Principal Components Analysis ...

  10. flume-ng源码阅读RollingFileSink(原创)

    org.apache.flume.sink.RollingFileSink    这个类比较简单. source的种类有两种:一种是PollableSource:另外一种是EventDrivenSou ...