原题链接在这里: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. 吐槽 MySQL数据库jdbc操作,varchar类型占位符问题——单引号造孽

    很长时间不写代码动手能力明显下降很多常见的错误还是经常发生,今天吐血了一次. 简单的坑总是要多跳几次才能甘心.很清晰的记得大学的时候在此坑差点闷死,现在又跳进这个坑了,搞了半天终于知道错在哪里. St ...

  2. ag-grid

    使用: import { AgGridVue } from "ag-grid-vue"; <ag-grid-vue style="width:100%;height ...

  3. 适配iOS9问题汇总

    iOS 9适配过程中出现的问题,收集的链接资料供大家学习分享. http://wiki.mob.com/ios9-对sharesdk的影响(适配ios-9必读)/ http://www.cocoach ...

  4. windows10添加电源计划修改的快捷方案

    转自:http://news.mydrivers.com/1/431/431346.htm 由于目前的Windows 10预览版在UI方面还未优化到位,所以某些设置选项要想找出来是很难的,这时候如果能 ...

  5. MySQL实时性能监控工具doDBA tools

    doDBA tools是什么? doDBA tools是一个基于控制台的远程监控工具,它不需要在本地/远程系统上安装任何软件,它可以实时收集操作系统.MySQL.InnoDB的实时性能状态数据,并可以 ...

  6. 偏远小渔村选手的noip2017游记

    这次noip估计是我初中最后一次比赛了,毕竟初三狗还要准备中考,要是中考挂了就GG了. 在最终成绩的榜上,我看到我成绩400,非常意外(你们这群大佬赛前天天奶我,还好不是毒奶),更意外的是全省竟然只有 ...

  7. 关于view里面xib的问题

    [[[NSBundle mainBundle] loadNibNamed:@"NetFailView" owner:self options:nil] lastObject]; 会 ...

  8. python正则表达式同时匹配多个关键字(多关键字匹配)

    网上翻了很多文章...居然没有一个有用的..倒是找到一篇java的,但java的正则表达式和python的还有点不同. 那篇java的文章是用"[keywd1]|[keywod2]|[key ...

  9. Linux系统用户/用户组/文件权限相关

    目录一.Linux系统用户/用户组权限相关二.Linux系统文件权限相关 一.Linux系统用户/用户组权限相关 .命令:usermod 用法:usermod [-agGus] user args ‘ ...

  10. compile to 32-bit elf file

    nasm -f elf -o a.o a.asm gcc -c -m32 -o b.o b.c ld -s -m elf_i386 -Ttext 0x30400 -o b.bin b.o a.o