Skip to content

Commit f9bf911

Browse files
committed
feat: update solutions to lcof questions
更新剑指 Offer 题解描述
1 parent c4b4e87 commit f9bf911

File tree

6 files changed

+12
-15
lines changed

6 files changed

+12
-15
lines changed

lcof/面试题58 - I. 翻转单词顺序/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
**注意:** 此题对比原题有改动
3838

3939
## 解法
40+
按空格分割字符串后逆序。
41+
4042
### Python3
4143
```python
4244
class Solution:

lcof/面试题60. n个骰子的点数/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@
3232

3333
假设 `dp[i][j]` 表示扔 i 个骰子,出现点数之和 j 的次数。n 个骰子,所以 i 的范围在 `1~n`,j 的范围在 `[1, 6n]`
3434

35-
单看第 n 枚骰子,它的点数可能为 `1,2,3,...,6`,因此扔完 n 枚骰子后点数之和 j 出现的次数,可以由扔完 n-1 枚骰子后,对应点数 j−1,j−2,j−3,...,j−6 出现的次数之和转化过来。即:
35+
单看第 n 枚骰子,它的点数可能为 `1,2,3,...,6`,因此扔完 n 枚骰子后点数之和 j 出现的次数,可以由扔完 n-1 枚骰子后,对应点数 `j−1,j−2,j−3,...,j−6` 出现的次数之和转化过来。即:
3636

3737
```
3838
for (第n枚骰子的点数 i = 1; i <= 6; i ++) {
3939
dp[n][j] += dp[n-1][j - i]
4040
}
4141
```
4242

43-
扔 1 枚骰子,点数可能是 1,2,3,4,5,6,且每个点数出现的次数均为 1。所以初始化如下:
43+
扔 1 枚骰子,点数可能是 `1,2,3,4,5,6`,且每个点数出现的次数均为 1。所以初始化如下:
4444

4545
```
4646
for (int j = 1; j <= 6; ++j) {

lcof/面试题64. 求1+2+…+n/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
- `1 <= n <= 10000`
2323

2424
## 解法
25+
递归,结合**逻辑与**短路运算符求解。
26+
2527
### Python3
2628
```python
2729
class Solution:

lcof/面试题65. 不用加减乘除做加法/README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818

1919
## 解法
2020
<!-- 这里可写通用的实现逻辑 -->
21-
对两数进行 `^` 异或运算,得到不进位的和;对两数进行 `&` 与运算,得到进位。循环,直至进位为 0。
21+
- 对两数进行按位 `^` 异或运算,得到不进位的和;
22+
- 对两数进行按位 `&` 与运算,然后左移一位,得到进位;
23+
- 循环,直至进位为 0。
2224

2325
### Python3
2426
<!-- 这里可写当前语言的特殊实现逻辑 -->
25-
由于 python int 是无限长整型,左移不会自动溢出,因此需要特殊处理。
27+
由于 python `int` 是无限长整型,左移不会自动溢出,因此需要特殊处理。
2628

2729
```python
2830
class Solution:

lcof/面试题68 - II. 二叉树的最近公共祖先/README.md

+1-5
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,7 @@ class Solution:
5252
return root
5353
left = self.lowestCommonAncestor(root.left, p, q)
5454
right = self.lowestCommonAncestor(root.right, p, q)
55-
if left is None:
56-
return right
57-
if right is None:
58-
return left
59-
return root
55+
return right if left is None else (left if right is None else root)
6056
```
6157

6258
### Java

lcof/面试题68 - II. 二叉树的最近公共祖先/Solution.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,10 @@
55
# self.left = None
66
# self.right = None
77

8-
98
class Solution:
109
def lowestCommonAncestor(self, root: TreeNode, p: TreeNode, q: TreeNode) -> TreeNode:
1110
if root is None or root == p or root == q:
1211
return root
1312
left = self.lowestCommonAncestor(root.left, p, q)
1413
right = self.lowestCommonAncestor(root.right, p, q)
15-
if left is None:
16-
return right
17-
if right is None:
18-
return left
19-
return root
14+
return right if left is None else (left if right is None else root)

0 commit comments

Comments
 (0)