原题链接在这里: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. C++中引用编译过的C代码为什么要用“extern c”

    函数经过编译系统的翻译成汇编,函数名对应着汇编标号.  因为C编译函数名与得到的汇编代号基本一样,如:fun()=>_fun, main=>_main  但是C++中函数名与得到的汇编代号 ...

  2. Docker容器技术-创建一个简单的Web应用

    一.创建一个简单的Web应用 1.identicon 基于某个值而自动产生的图像,这个值是IP地址或用户名的散列值. 用途: 通过计算用户名或IP地址的散列值,在网站上提供用于识别用户的图像,以及自动 ...

  3. 数独C语言算法

    备好:http://blog.chinaunix.net/uid-26456800-id-3380612.html

  4. QT线程

    一.QObject子类 说明:以串口线程传输文件为例子,使用的是MoveTothread函数. void QObject::moveToThread(QThread *targetThread)可以将 ...

  5. poj 2762 Going from u to v or from v to u?【强连通分量缩点+拓扑排序】

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15812 ...

  6. JavaScrip 原生多文件上传及预览 兼容多浏览器

    JavaScrip 原生多文件上传及预览 兼容多浏览器 html代码块 <div class="container"> <label>请选择一个图像文件:& ...

  7. 使用Blob获取图片并二进制显示实例页面

    HTML代码: <div id="forAppend" class="demo"></div> JS代码: var eleAppend ...

  8. c#线程中下载文件到本地

    额,太懒了 直接上示例代码... /// <summary> /// 下载文件到本地 2017-05-31 /// </summary> /// <param name= ...

  9. getBytes()详解

    在java中,getBytes()方法如果不指定字符集,则得到的是一个操作系统默认的编码格式的字节数组:如果指定字符集,则得到的是在指定字符集下的字节数组,如: byte[] b_gbk = &quo ...

  10. AI探索(二)Tensorflow环境准备

    Python + Tensorflow环境安装 Tensorflow支持Windows/Mac/Linux等三种操作系统, 其中windows下python需要安装3.5以上的版本 Mac/Linux ...