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].

  

解题:题意很容易理解,将十进制整型数转化为七进制的数,并以字符串的形式返回。先看我写的第一种方法,比较繁琐,也利用了StringBuffer和堆栈,代码如下:

 class Solution {
public String convertToBase7(int num) {
Stack<Integer>stack = new Stack<Integer>();
boolean isNagative = false;
if(num < 0){
isNagative = true;
num = -num;
}
while(num != 0){
stack.push(num % 7);
num /= 7;
}
StringBuffer result = new StringBuffer(""); while(!stack.isEmpty()) result.append(String.valueOf(stack.pop()));
if(result.length() == 0)
return String.valueOf(0);
else if(isNagative)
return '-'+result.toString();
else return result.toString();
}
}

稍微改进一下,去掉栈和StringBuffer,直接使用String及其性质,速度会快很多,代码如下:

 class Solution {
public String convertToBase7(int num) {
boolean isNagative = false;
String result = "";
if(num < 0){
isNagative = true;
num = -num;
}
while(num != 0){
result = String.valueOf(num % 7) + result;
num /= 7;
}
if(result.length() == 0)
return String.valueOf(0);
else if(isNagative)
return '-'+result;
else return result;
}
}

当然也可以这样,虽然有点投机取巧:

  public String convertToBase7(int num) {
return Integer.toString(num, 7);
}

504. Base 7的更多相关文章

  1. 45. leetcode 504. Base 7

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

  2. 【leetcode】504. Base 7

    problem 504. Base 7 solution: class Solution { public: string convertToBase7(int num) { ) "; st ...

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

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

  4. [LeetCode] 504. Base 7_Easy tag: Math

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

  5. LeetCode 504. Base 7 (C++)

    题目: Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "2 ...

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

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

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

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

  8. 504 Base 7 七进制数

    给定一个整数,将其转化为7进制,并以字符串形式输出.示例 1:输入: 100输出: "202" 示例 2:输入: -7输出: "-10"注意: 输入范围是 [- ...

  9. C#版 - Leetcode 504. 七进制数 - 题解

    C#版 - Leetcode 504. 七进制数 - 题解 Leetcode 504. Base 7 在线提交: https://leetcode.com/problems/base-7/ 题目描述 ...

随机推荐

  1. UNIX高级环境编程(16)文件系统 < 雨后 >

    来点绿色放松一下眼睛吧 :) 文件系统是对文件和目录的组织集合. 一 设备文件 设备文件和系统的某个设备相对应. 设备驱动程序 处理设备的所有IO请求. 提供了一致的API接口,对应于系统调用的ope ...

  2. ETL技巧应用(高级应用介绍:准备区运用、 时间戳的运用、日志表的运用、使用调度)

    1.1    准备区运用 a.在构建数据仓库时,数据源位于一服务器上,数据仓库在另一服务器端,数据源Server端访问频繁,并且数据量大,需要不断更新, b.建立准备区数据库: >将数据抽取到准 ...

  3. opengl 实体和网格绘图函数(基础)(转)

    http://blog.csdn.net/he_wen_jian/article/details/8594880 GLUT工具箱提供几种图形3维图形的函数: void glutWireSphere(G ...

  4. 用户不再sudoers文件中

    1.修改/etc/sudoers文件权限 # chmod 777 /etc/sudoers 2.编辑/etc/sudoers文件,添加要提升权限的用户: 在文件中找到root ALL=(ALL) AL ...

  5. Apache的权限设置与构建虚拟web主机

    实验拓扑图: 实验要求: 1.  搭建WEB服务器,能访问默认站点,并使用awstats软件能监控到默认站点的访问情况. 2.  修改Apache的主配置文件,设置1.10只能访问awstats网站, ...

  6. October 20th 2017 Week 42nd Friday

    My life is in these books. Read these and know my heart. 我的人生就在这些书中,读完他们就能读懂我的心. Some people say tha ...

  7. Spring Boot 集成 thymeleaf 模版引擎

    Spring Boot 建议使用 HTML 来完成动态页面.Spring Boot 提供了大量的模版引擎,包括 Thymeleaf.FreeMarker.Velocity等. Spring Boot ...

  8. Redis上踩过的一些坑

    来自: http://blog.csdn.net//chenleixing/article/details/50530419 上上周和同事(龙哥)参加了360组织的互联网技术训练营第三期,美团网的DB ...

  9. HTML5音/视频标签详解

    一.发展历: 早期:<embed>+<object>+文件   问题:不是所有浏览器都支持,而且embed不是标准.   现状:Realplay.window media.Qu ...

  10. 1083. [SCOI2005]繁忙的都市【最小生成树】

    Description 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道 路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个交叉路 ...