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. Windows解决端口被占用问题

    第一种解决方法,以8080端口为例 打开命令行输入 cmd ,输入netstat -ano 会显示所有已经在运行的端口情况.PID为进程id 输入你想要查的正在占用的端口号,netstat -ano ...

  2. PySpark 的背后原理--在Driver端,通过Py4j实现在Python中调用Java的方法.pyspark.executor 端一个Executor上同时运行多少个Task,就会有多少个对应的pyspark.worker进程。

    PySpark 的背后原理 Spark主要是由Scala语言开发,为了方便和其他系统集成而不引入scala相关依赖,部分实现使用Java语言开发,例如External Shuffle Service等 ...

  3. php中array的常用操作示码

    融会了,也就熟悉了. 这事得多练,多改. <?php $empty1 = []; $empty2 = array(); $names = ['Harry', 'Ron', 'Hermione'] ...

  4. G1垃圾收集器系统化说明【官方解读】

    还是继续G1官网解读,上一次已经将这三节的东东读完了,如下: 所以接一来则继续往下读: Reviewing Generational GC and CMS[回顾一下CMS收集器] The Concur ...

  5. .NET下各种可用的HTML解析组件

    做数据抓取,网络爬虫方面的开发,自然少不了解析HTML源码的操作.那么问题来了,到底.NET如何来解析HTML,有哪些解析HTML源码的好用的,有效的组件呢?   作者在开始做这方面开发的时候就被这些 ...

  6. 项目Alpha冲刺 9

    作业描述 课程: 软件工程1916|W(福州大学) 作业要求: 项目Alpha冲刺(团队) 团队名称: 火鸡堂 作业目标: 介绍第9天冲刺的项目进展.问题困难和心得体会 1.团队信息 队名:火鸡堂 队 ...

  7. HTTP Status 500 - DateConverter does not support default String to 'Date' conversion.错误

    //自己指定一个类型转换器(将String转成Date) DateConverter converter = new DateConverter(); converter.setPattern(new ...

  8. TAPD---“文档”的用途

    主要用途:文件的存放 (1)对于测试组:存放测试用例.主要针对当前的迭代,可新建对应的文件夹,上传存放相应的xmind.excel文件.方便开发查找用例文件 (2)对于项目:存放共用的文档等 这里只是 ...

  9. gitlab修改ip

    gitlab 修改ip的两种方式: 修改/etc/gitlab/gitlab.rd 里面的#external_url 'http://gitlab.example.com' 为ip地址,然后重新构建- ...

  10. 【Spring】如何配置多个applicationContext.xml文件

    在web.xml中通过contextConfigLocation配置spring 开发Java Web程序,使用ssh架构时,默认情况下,Spring的配置文件applicationContext.x ...