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

给一个整数,返回它的七进制数。

解法1: 迭代

解法2: 递归

Java:

public class Solution {
public String convertTo7(int num) {
if (num == 0) return "0"; StringBuilder sb = new StringBuilder();
boolean negative = false; if (num < 0) {
negative = true;
}
while (num != 0) {
sb.append(Math.abs(num % 7));
num = num / 7;
} if (negative) {
sb.append("-");
} return sb.reverse().toString();
}
} 

Java:

public String convertTo7(int num) {
if (num < 0)
return '-' + convertTo7(-num);
if (num < 7)
return num + "";
return convertTo7(num / 7) + num % 7;
}  

Python:

class Solution(object):
def convertToBase7(self, num):
"""
:type num: int
:rtype: str
"""
if num == 0:
return '0'
if num < 0:
return '-' + self.convertToBase7(-num) res = ''
while num > 0:
res = str(num % 7) + res
num = num / 7 return res

Python:

def convertTo7(self, num):
if num == 0: return '0'
n, res = abs(num), ''
while n:
res = str(n % 7) + res
n //= 7
return res if num > 0 else '-' + res  

Python:

def convertTo7(self, num):
if num < 0: return '-' + self.convertTo7(-num)
if num < 7: return str(num)
return self.convertTo7(num // 7) + str(num % 7)  

C++:

class Solution {
public:
string convertToBase7(int num) {
if (num == 0) return "0";
string res = "";
bool positive = num > 0;
while (num != 0) {
res = to_string(abs(num % 7)) + res;
num /= 7;
}
return positive ? res : "-" + res;
}
};

C++:  

class Solution {
public:
string convertToBase7(int num) {
if (num < 0) return "-" + convertToBase7(-num);
if (num < 7) return to_string(num);
return convertToBase7(num / 7) + to_string(num % 7);
}
};

  

  

All LeetCode Questions List 题目汇总

[LeetCode] 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] Base 7 基数七

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

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

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

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

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

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

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

  6. 【leetcode】504. Base 7

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

  7. Java实现 LeetCode 504 七进制数

    504. 七进制数 给定一个整数,将其转化为7进制,并以字符串形式输出. 示例 1: 输入: 100 输出: "202" 示例 2: 输入: -7 输出: "-10&qu ...

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

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

  9. [递归回溯] LeetCode 504七进制数(摸鱼版)

    LeetCode 七进制数 前言: 这个就没什么好说的了 题目:略 步入正题 进位制转换 10 -n 余数加倒叙 没什么好讲的直接上七进制代码 偷个懒 10进位制转7 class Solution { ...

随机推荐

  1. Codeforces B. Minimum Possible LCM(贪心数论)

    题目描述: B. Minimum Possible LCM time limit per test 4 seconds memory limit per test 1024 megabytes inp ...

  2. python笔记42-http请求命令行工具(httpie)

    前言 通常我们需要快速的测试某个接口通不通,一般linux上用curl去发http请求,但是这个命令行工具语法有点复杂了,不够直观. python有一个给人类使用的requests库,非常的简单方便. ...

  3. springboot+Mybatis+MySql 一个update标签中执行多条update sql语句

    Mysql是不支持这种骚操作的,但是不代表并不能实现,只需要在jdbc配置文件中稍微做一下修改就行. driver=com.mysql.jdbc.Driver url=jdbc:mysql://127 ...

  4. CSE301 – Bio-Computation

    CSE301 – Bio-Computation Assessment 3Contribution to overall module assessment 10%Submission deadlin ...

  5. JQuery系列(3) - 工具方法

    jQuery函数库提供了一个jQuery对象(简写为$),这个对象本身是一个构造函数,可以用来生成jQuery对象的实例.有了实例以后,就可以调用许多针对实例的方法,它们定义jQuery.protot ...

  6. PHP——仿造微信OpenId

    前言 这就是拿来玩的,其次假的就是假的,成不了真的! 代码 首先我观察了两个公众号关注后的生成openid的规则,发现了以下规则 1. OpenID都是28位 2. 前六位是有规律的 然后接下来就按着 ...

  7. 使用selenium谷歌浏览器驱动配置:

    from selenium import webdriver#导入谷歌浏览器的chrome_driverchrome_driver = r"C:\python36\Lib\site-pack ...

  8. HTML5 自定义属性 data-*属性名一定要小写吗?

    最近学习 javascript ,参考书籍是< javascript 高级程序设计>第三版,在介绍自定义元素属性时书中给出了一个例子,如下:<div id="myDiv&q ...

  9. Codeforces1114C Trailing Loves (or L'oeufs?)

    链接:http://codeforces.com/problemset/problem/1114/C 题意:给定数字$n$和$b$,问$n!$在$b$进制下有多少后导零. 寒假好像写过这道题当时好像完 ...

  10. L1141

    一,看题 1,位于0格可移动到相邻得1格.位于1格可移动到相邻的0格上. 2,从某一格开始可以移动的格子数.(应该不能重复,否则不久循环了.那就意味着我们可以要标记喽?) 3 二,写题 1,你是一次一 ...