Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d57bd03

Browse files
罗文斌罗文斌
罗文斌
authored and
罗文斌
committed
[update](2023年05月05日 16:55:21): auto commit and push
1 parent 6a32a60 commit d57bd03

File tree

4 files changed

+64
-36
lines changed

4 files changed

+64
-36
lines changed

‎.DS_Store‎

0 Bytes
Binary file not shown.

‎algorithm_templates/.DS_Store‎

2 KB
Binary file not shown.

‎algorithm_templates/common/common.py‎

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
# 心得:
3+
# 主要想清楚几点问题就行了
4+
# 1.这个问题的子问题是什么。
5+
# 2.当前层要干什么事情。
6+
# 3.递归出口 【边界条件】
7+
8+
# 很多刚接触递归的同学习惯往深处想,就想想清楚下一层,
9+
# 下下层到底咋回事,千万别!
10+
# 这样永远也想不清楚的,
11+
# 你只要把当前层的事情干好,
12+
# 边界条件找好,
13+
# 深层自然而然就是对的。
14+
15+
16+
# 231 :https://leetcode.cn/problems/power-of-two/description/
17+
# 2 的幂
18+
19+
class Solution:
20+
def isPowerOfTwo(self, n) -> bool:
21+
if n == 1:
22+
return True
23+
if n <= 0 or int(n) != n:
24+
return False
25+
else: ##开始递归
26+
return self.isPowerOfTwo(n / 2)
27+
28+
29+
## 算阶乘
30+
def jiecheng(x):
31+
# sum=1
32+
# while x>0:
33+
# sum=x*jiecheng(x-1)
34+
# x-=1
35+
# if x==0:
36+
# return 1
37+
# return sum
38+
if x==0:
39+
return 1
40+
else: ##开始递归
41+
return x*jiecheng(x-1)
42+
43+
44+
## 个位想加
45+
## https://leetcode.cn/problems/add-digits/description/
46+
# 输入: num = 38
47+
# 输出: 2
48+
# 解释: 各位相加的过程为:
49+
# 38 --> 3 + 8 --> 11
50+
# 11 --> 1 + 1 --> 2
51+
# 由于 2 是一位数,所以返回 2。
52+
53+
class Solution(object):
54+
def addDigits(self, num):
55+
"""
56+
:type num: int
57+
:rtype: int
58+
"""
59+
new_num = reduce(lambda x,y:x+y,map(int,list(str(num))))
60+
if num>=10: ##开始递归
61+
return self.addDigits(new_num)
62+
else:
63+
return reduce(lambda x,y:x+y,map(int,list(str(num))))
64+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /