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. Cisco HSRP 配置方法(热备份路由协议)配置实例

    转裁于51CTO.http://www.mamicode.com/info-detail-862350.html HSRP----热备份路由协议 思科私有协议,与VRRP 虚拟路由协议 相近,(国际标 ...

  2. 【Alpha 冲刺】 3/12

    今日任务总结 (未完成) 人员 今日原定任务 完成情况 遇到问题 贡献值 胡武成 完成API文档编写 已完成App端api,Web端api还在持续 时间紧 孙浩楷 理解掌握在线编辑插件使用 加深了对所 ...

  3. 数据库事务总结(一)-ACID

    概述 数据库事务(Database Transaction) ,是指作为单个逻辑工作单元执行的一系列操作. 事务处理可以确保除非事务性单元内的所有操作都成功完成,否则不会永久更新面向数据的资源.通过将 ...

  4. 【Python求助】在eclipse和pycharm中,通过adb install安装中文名字APK时老是报错,如何解决

    # -*- coding: utf-8 -*- import os import sys import subprocess import time from uiautomator import d ...

  5. Python接口自动化--Json数据处理 5

    1.Json模块简介,全名JavaScript Object Notation,轻量级的数据交换格式,常用于http请求中. Encoding basic Python object hierarch ...

  6. bootstrap模态框input不能获取焦点并编辑【转】

    Bootstrap模态框时input标签[日期控件也有这样的问题]不能编辑的问题,下面是我的解决方法: 1.将下图中框出来的属性删掉即可: 2.兼容火狐浏览器,笔者在火狐中还是不能编辑,去掉下图属性即 ...

  7. 【洛谷】【单调栈】P1823 音乐会的等待

    [题目描述:] N个人正在排队进入一个音乐会.人们等得很无聊,于是他们开始转来转去,想在队伍里寻找自己的熟人.队列中任意两个人A和B,如果他们是相邻或他们之间没有人比A或B高,那么他们是可以互相看得见 ...

  8. C# winform单元格的formatted值的类型错误 DataGridView中CheckBox列运行时候System.FormatException异常

    在DataGridView手动添加了CheckBox列;在窗体Show的时候,遇到一个错误:错误如下: DataGridView中发生一下异常:System.FormatException:单元格的F ...

  9. treap入门

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

  10. java中Integer与int装箱拆箱一点收获

    示例代码: class BoxIntInteger { public static void main(String[] args) { Integer a = new Integer(10111); ...