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. Selenium+TestNG+CSV数据驱动

    1.工程的目录结构: # saas SAAS_UI自动化测试工程 # 一.工程的目录结构 1.saas/src是工程的入口 a.saas/src/main/java/com/saas/encapsul ...

  2. luoguU60884 【模板】动态点分治套线段树

    题目连接:https://www.luogu.org/problemnew/show/U60884 题意:有N个点,标号为1∼N,用N−1条双向带权通道连接,保证任意两个点能互相到达. Q次询问,问从 ...

  3. 数据库jdbc链接:mysql, oracle, postgresql

    #db mysql#jdbc.driver=com.mysql.jdbc.Driver#jdbc.url=jdbc:mysql://localhost:3306/mysql?&useUnico ...

  4. nginx设置反向代理,获取真实客户端ip

    upstream这个模块提供一个简单方法来实现在轮询和客户端IP之间的后端服务器负荷平衡. upstream abc.com { server 127.0.0.1:8080; server 127.0 ...

  5. apache commons-configuration包读取配置文件

    1.pom依赖添加 <!-- 配置文件读取 --> <dependency> <groupId>commons-configuration</groupId& ...

  6. JS获取本周、本季度、本月、上月、本年的开始日期、结束日期

    /** * 获取本周.本季度.本月.上月的开始日期.结束日期 */ var now = new Date(); //当前日期  var nowDayOfWeek = now.getDay(); //今 ...

  7. 关于System.BadImageFormatException

    什么是BadImageFormatException BadImageFormatException是当动态链接库 (DLL) 或可执行程序的文件映像无效时引发的异常. 可能的原因 如果动态链接库 ( ...

  8. Python 08 skimage

    原文:https://www.cnblogs.com/xdjun/p/7874794.html 命令: pip install numpy pip install scipy pip install ...

  9. 【loj3123】【CTS2019】重复

    题目 给出一个长度为\(n\)的串\(s\),询问有多少个长度为\(m\)的串\(t\) 满足 \(t\) 的无限循环串存在一个长度为\(n\)且比\(s\)字典序严格小的子串 $ n , m \le ...

  10. Calibre中使用DeDRM插件进行Kindle电子书解锁

    小书匠 废话不多说,下面是Calibre和DeDRM插件的下载地址: https://calibre-ebook.com/download https://github.com/apprenticeh ...