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. sshpass 绕过ssh 密码交互式验证

    经常我们使用脚本登入服务器的时候,如果使用ssh 命令,经常会提示密码输入,我们不得不手动输入密码,因为ssh 没有 密码的选项. 为了能绕过交互式验证,我们使用sshpass命令解决这个问题: 1. ...

  2. Window10 Linux子系统挂载磁盘

    默认情况下, Linux子系统将当前winodws磁盘的盘全部挂载到/mnt/<disk_label>, 但一些新增的盘就需要手动做下了.. 官方参考文档 挂载磁盘 -- DrvFs 挂载 ...

  3. 一、HttpServletRequest接口 二、HttpServletReponse接口 三、POST和GET请求方式及其乱码处理 四、ServletContext对象和ServletConfig对象

    一.HttpServletRequest接口 内部封装了客户端请求的数据信息 接收客户端的请求参数.HTTP请求数据包中配置参数 ###<1>常用方法 getContextPath()重要 ...

  4. loli的混合算法测试

    最近刚讲了最短路,说要考试我以为是考最短路,然而只有一道是最短路... 数据似乎有一点问题,不管了,反正手工测评都是对的,那现在就来看看题吧. Balanced:(此处并没有网址) 题意概述:$n$  ...

  5. BZOJ2502:清理雪道(有上下界最小流)

    Description        滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道),弧的方向代表斜坡下降的方向. 你的团队负责每周定时 ...

  6. Eclipse Mars 2安装Drools6.4插件(Drools and jBPM tools)时无法安装JBoss Runtime Drools Detector

    在eclipse上本地安装Drools6.4Final的时候出现两个组件无法正常安装的情况,具体组件如下: 具体的提示信息为: Cannot complete the install because ...

  7. Inter exchange Client Address Protocol (ICAP)- 互换客户端地址协议

    https://github.com/ethereum/wiki/wiki/Inter-exchange-Client-Address-Protocol-(ICAP) Transferring fun ...

  8. [转]opengl入门例题(读取bmp图片,并显示)

    #include<gl/glut.h> #define FileName "bliss.bmp" static GLint imagewidth; static GLi ...

  9. Python2.7-zlib

    zlib 模块,提供了压缩和解压缩的函数,都是对字符串进行操作的,可以理解为是各种不同类型的数据经过处理成为字符串或是二进制字符串,再进行压缩解压缩.是和 gzip 相兼容的压缩模块 模块方法: zl ...

  10. HDU 3861 The King’s Problem(tarjan缩点+最小路径覆盖:sig-最大二分匹配数,经典题)

    The King’s Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...