[LeetCode] 504. Base 7 基数七
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 基数七的更多相关文章
- 45. leetcode 504. Base 7
504. Base 7 Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: ...
- [LeetCode] Base 7 基数七
Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "202&q ...
- [LeetCode] 504. Base 7_Easy tag: Math
Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "202&q ...
- LeetCode 504. Base 7 (C++)
题目: Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "2 ...
- C#版 - Leetcode 504. 七进制数 - 题解
C#版 - Leetcode 504. 七进制数 - 题解 Leetcode 504. Base 7 在线提交: https://leetcode.com/problems/base-7/ 题目描述 ...
- 【leetcode】504. Base 7
problem 504. Base 7 solution: class Solution { public: string convertToBase7(int num) { ) "; st ...
- Java实现 LeetCode 504 七进制数
504. 七进制数 给定一个整数,将其转化为7进制,并以字符串形式输出. 示例 1: 输入: 100 输出: "202" 示例 2: 输入: -7 输出: "-10&qu ...
- 【LeetCode】504. Base 7 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 内建库 BigInteger类 逐位计算 倍数相加 ...
- [递归回溯] LeetCode 504七进制数(摸鱼版)
LeetCode 七进制数 前言: 这个就没什么好说的了 题目:略 步入正题 进位制转换 10 -n 余数加倒叙 没什么好讲的直接上七进制代码 偷个懒 10进位制转7 class Solution { ...
随机推荐
- O(n) 取得数组中每个元素右边第一个比它大的元素
题目: 给定一个整型数组,数组元素随机无序的,要求打印出所有元素右边第一个大于该元素的值. 如数组A=[6,8,9,2,3,5,6] 输出[8,9,-1,3,5,6,-1] 思路: 我们用栈来保存未找 ...
- 使用CefSharp在C#访问网站,支持x86和x64
早已久仰CefSharp大名,今日才得以实践,我其实想用CefSharp来访问网站页面,然后抓取html源代码进行分析,如果使用自带的WebBrowser控件,可能会出现一些不兼容js的错误. Cef ...
- 通过ip找mac
# coding:utf-8 import os cmd = {'arp': 'arp -a | find "', 'route': 'route PRINT ' } def win_mac ...
- 基于Helm和Operator的K8S应用管理
https://blog.csdn.net/RancherLabs/article/details/79483013 大家好,今天我们分享的内容是基于Helm和Operator的K8S应用管理. 我们 ...
- 英语听力,如何成为更好的交谈着https://www.bilibili.com/video/av4279405?from=search&seid=5889429711390689339
and how many of you know at least one person that you because you just do not want to talk to them.y ...
- Cramer-Rao Bounds (CRB)
克拉美-罗界.又称Cramer-Rao lower bounds(CRLB),克拉美-罗下界. 克拉美罗界是对于参数估计问题提出的,为任何无偏估计量的方差确定了一个下限.无偏估计量的方差只能无限制的逼 ...
- (尚027)Vue_案例_交互添加
TodoHeader.vue组件 写交互: 第一步:跟目标元素绑定监听 (1).按回车键确认@keyup.enter="add" (2). 注意:数据在哪个组件,更新数据的行为就应 ...
- (浙江金华)Day 1 组合数计数
目录 Day 1 组合计数 1.组合数 (1).C(n,m) 读作n选m,二项式系数 : (2).n个东西里选m个的方案数 不关心选的顺序: (3).二项式系数--->多项式系数: 2.组合数计 ...
- shell脚本编程基础之case语句
基础简介 脚本编程分为: 面向过程 选择结构:if语句,单分支.双分支.多分支:case语句 控制结构:顺序结构(默认) 循环结构:for.while.until 面向对象 case语句结构 case ...
- 微信小程序 movable-view组件应用:可拖动悬浮框_返回首页
1. movable-view组件具体内容可参考官网:微信官方文档 2. demo参考:https://github.com/ChinaFanny/YFWeappMovableView 运行效果 核心 ...