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. 怎么查看自己电脑的IP地址

    1/2 使用Windows+R键打开“运行”窗口,然后输入CMD进入命令提示窗口 进入命令窗口之后,输入:ipconfig/all 回车即可看到整个电脑的详细的IP配置信息 1/3 使用网络状态查看I ...

  2. WinThruster清理电脑注册表

    电脑因为安装卸载各种软件,长时间工作,越来越卡慢,很大程度上和电脑中一些老旧不用的注册表有关,一些遗留问题也成为隐患. 今天我们主要来讲一下一些不用,没用的注册表清理问题. 无意间在网上看了一个软件, ...

  3. 微信支付回调,XXE攻击漏洞防止方法

    最近微信支付回调发现的XXE攻击漏洞(什么是XXE攻击,度娘.bing去搜,一搜一大把),收到通知后即检查代码, 微信给的解决方法是如果你使用的是: XmlDocument: XmlDocument ...

  4. Pre标签 自动换行

    <pre> 元素可定义预格式化的文本.被包围在 pre 元素中的文本通常会保留空格和换行符.而文本也会呈现为等宽字体. <pre> 标签的一个常见应用就是用来表示计算机的源代码 ...

  5. 带有function的JSON对象的序列化与还原

      JSON对象的序列化与反序列化相信大家都很熟悉了.基本的api是JSON.parse与JSON.stringify. var json={ uiModule:'http://www.a.com', ...

  6. 13.2SolrCloud集群使用手册之CoreAdmin API

    转载请出自出处:http://www.cnblogs.com/hd3013779515/ CoreAdminHandler是用来管理Solr cores的,用来管理一个Solr instance中所有 ...

  7. 关于flex的crossdomain.xml文件存放目录

    最近在项目中遇到flex跨域访问的安全沙箱问题,查资料了解到需要在服务端加上crossdomain.xml文件,即: <?xml version="1.0" encoding ...

  8. QT5 视图坐标

    又出错了. . main.obj:-1: error: LNK2001: 无法解析的外部符号 "public: virtual struct QMetaObject const * __th ...

  9. 2.2.1 LinearLayout(线性布局)

    本节引言 本节开始讲Android中的布局,Android中有六大布局,分别是: LinearLayout(线性布局), RelativeLayout(相对布局), TableLayout(表格布局) ...

  10. numpy库数组拼接np.concatenate的用法

    concatenate功能:数组拼接 函数定义:numpy.concatenate((a1, a2, ...), axis=0, out=None)