Skip to content

Commit 7aa6fd4

Browse files
author
lucifer
committed
feat: 504
1 parent 9688a0e commit 7aa6fd4

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
187187
- [401. 二进制手表](./problems/401.binary-watch.md)
188188
- [0437. 路径总和 III](./problems/437.path-sum-iii.md)
189189
- [0455. 分发饼干](./problems/455.AssignCookies.md)
190+
- [0504. 七进制数](./problems/504.base-7.md) 🆕
190191
- [0575. 分糖果](./problems/575.distribute-candies.md)
191192
- [0665. 非递减数列](./problems/665.non-decreasing-array.md) 🆕
192193
- [821. 字符的最短距离](./problems/821.shortest-distance-to-a-character.md) 91

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
- [401. 二进制手表](problems/401.binary-watch.md)
8585
- [0437. 路径总和 III](problems/437.path-sum-iii.md)
8686
- [0455. 分发饼干](problems/455.AssignCookies.md)
87+
- [0504. 七进制数](./problems/504.base-7.md) 🆕
8788
- [0575. 分糖果](problems/575.distribute-candies.md)
8889
- [0665. 非递减数列](./problems/665.non-decreasing-array.md) 🆕
8990
- [821. 字符的最短距离](problems/821.shortest-distance-to-a-character.md) 91

problems/504.base-7.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
## 题目地址(504. 七进制数)
2+
3+
https://leetcode-cn.com/problems/base-7/
4+
5+
## 题目描述
6+
7+
```
8+
给定一个整数,将其转化为7进制,并以字符串形式输出。
9+
10+
示例 1:
11+
12+
输入: 100
13+
输出: "202"
14+
15+
16+
示例 2:
17+
18+
输入: -7
19+
输出: "-10"
20+
21+
22+
注意: 输入范围是 [-1e7, 1e7] 。
23+
```
24+
25+
## 前置知识
26+
27+
-
28+
29+
## 公司
30+
31+
- 暂无
32+
33+
## 思路
34+
35+
这道题很经典也很重要。 如果你把这道题搞懂了,那么所有的进制转化题目对你来说都不是问题。 另外有的题目虽然不是直接让你进制转化,不过使用进制转化却实实在在可以优化代码。
36+
37+
10 进制转化任意进制的思路都是**除 x 取余**,其中 x 为进制数,比如 2 进制就是 除 2 取余,7 进制就是除 7 取余。这个大家可能都知道,这里再带大家回顾一下。
38+
39+
比如一个数 4321 ,需要转化为 7 进制。那么可以:
40+
41+
- 先将 4321 除以 7,其中余数为 0 , 除数为 616
42+
- 继续将 616 采用同样的方法直到小于 7
43+
44+
将此过冲的余数**反序**就是答案了。图解:
45+
46+
![](https://tva1.sinaimg.cn/large/008eGmZEly1goaco026g7j30pe0zb40r.jpg)
47+
(图片来自网络)
48+
49+
如图,4312 的 7 进制就是 15400。
50+
51+
## 关键点
52+
53+
- 除 x 取余,并逆序输出
54+
55+
## 代码
56+
57+
- 语言支持:Python3
58+
59+
Python3 Code:
60+
61+
递归:
62+
63+
```python
64+
65+
class Solution:
66+
def convertToBase7(self, num: int) -> str:
67+
if num < 0:
68+
return "-" + self.convertToBase7(-num)
69+
if num < 7:
70+
return str(num)
71+
return self.convertToBase7(num // 7) + str(num % 7)
72+
73+
```
74+
75+
**复杂度分析**
76+
77+
令 n 为数组长度。
78+
79+
- 时间复杂度:$O(n)$
80+
- 空间复杂度:$O(h)$,其中 h 为递归栈的深度。
81+
82+
迭代:
83+
84+
```py
85+
class Solution:
86+
def convertToBase7(self, num: int) -> str:
87+
if num == 0:
88+
return 0
89+
ans = []
90+
is_negative = num < 0
91+
num = abs(num)
92+
while num > 0:
93+
num, remain = num // 7, num % 7
94+
ans.append(str(remain))
95+
96+
return "-" + "".join(ans[::-1]) if is_negative else "".join(ans[::-1])
97+
98+
```
99+
100+
**复杂度分析**
101+
102+
令 n 为数组长度。
103+
104+
- 时间复杂度:$O(n)$
105+
- 空间复杂度:$O(1)$
106+
107+
> 此题解由 [力扣刷题插件](https://leetcode-pp.github.io/leetcode-cheat/?tab=solution-template) 自动生成。
108+
109+
力扣的小伙伴可以[关注我](https://leetcode-cn.com/u/fe-lucifer/),这样就会第一时间收到我的动态啦~
110+
111+
以上就是本文的全部内容了。大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 40K star 啦。大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。
112+
113+
关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。
114+
115+
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)

0 commit comments

Comments
 (0)