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. gnome-shell 使用 notify-send 发送桌面消息

    什么是notify-send? notify-send - a program to send desktop notifications 怎么使用? NAME notify-send - a pro ...

  2. python基础学习2

    一.算数运算符 +加法,-减法,*乘法,/除法,//地板除,%求余,**幂运算. 二.逻辑运算符 非not.且and.或or.优先级依次为not,and,or. 三.print()end结尾 prin ...

  3. 【转】Linxu学习---top实践

    [原文]https://www.toutiao.com/i6591053058258502147/ 在实际开发中,有时候会收到一些服务的监控报警,比如CPU飙高,内存飙高等,这个时候,我们会登录到服务 ...

  4. PHPStorm 中配置 XDebug

    1.下载 Xdebug ps : php版本和xdebug版本一定要相对应 如果不知道下载哪个版本,将phpinfo网页的源代码拷贝到https://xdebug.org/wizard.php,然后按 ...

  5. Docker容器学习与分享04

    Docker容器的基本操作(2) 基于docker分享03的centos容器,接着学习docker容器的基本操作. docker分享03中创建了一个centos镜像,如果想要查看容器的具体信息就要使用 ...

  6. vbs常用函数

    aa '删除文件夹 sub DeleteFolder(objFolder) call OutputLog(objFolder.Path,true) err.Clear On Error Resume ...

  7. Asp.net中DataTable的排序功能

    DataTable里的数据,如果是从数据库中取得的数据,我们可以用order by排序,而从excel表格取得的数据,就需要自己进行操作了. 例如,Dt_Data2是读取Excel表格取到的数据 Da ...

  8. .Net使用163smtp发送邮件时错误:邮箱不可用. has no permission解决方法

    C#实现简单邮件发送代码如下 public static void SendAsync(string emailTo, string subject, string mailBody) { var m ...

  9. objc.io 待看文章

    https://objccn.io/issues/ https://objccn.io/issues/ 使用 VIPER 构建 iOS 应用 并发编程

  10. treap入门

    这几天刚学了treap,听起来还行,就是调题调到恶心了…… 就以这道题作为板子吧(”你本来也就做了一道题!”) https://www.luogu.org/problemnew/show/P3369 ...