Skip to content

Commit 44a6967

Browse files
authored
feat: update solutions to lcof problems (#2908)
1 parent 0af6fc9 commit 44a6967

File tree

49 files changed

+747
-874
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+747
-874
lines changed

lcof/面试题53 - I. 在排序数组中查找数字 I/README.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcof/%E9%9D%A2%E8%AF%95%E9
5252

5353
由于数组 `nums` 已排好序,我们可以使用二分查找的方法找到数组中第一个大于等于 `target` 的元素的下标 $l$,以及第一个大于 `target` 的元素的下标 $r$,那么 `target` 的个数就是 $r - l$。
5454

55-
时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。其中 $n$ 为数组的长度
55+
时间复杂度 $O(\log n)$,其中 $n$ 为数组的长度。空间复杂度 $O(1)$。
5656

5757
<!-- tabs:start -->
5858

@@ -70,13 +70,16 @@ class Solution:
7070

7171
```java
7272
class Solution {
73+
private int[] nums;
74+
7375
public int search(int[] nums, int target) {
74-
int l = lowerBound(nums, target);
75-
int r = lowerBound(nums, target + 1);
76+
this.nums = nums;
77+
int l = search(target);
78+
int r = search(target + 1);
7679
return r - l;
7780
}
7881

79-
private int lowerBound(int[] nums, int x) {
82+
private int search(int x) {
8083
int l = 0, r = nums.length;
8184
while (l < r) {
8285
int mid = (l + r) >>> 1;
@@ -108,8 +111,8 @@ public:
108111
109112
```go
110113
func search(nums []int, target int) int {
111-
l := sort.Search(len(nums), func(i int) bool { return nums[i] >= target })
112-
r := sort.Search(len(nums), func(i int) bool { return nums[i] > target })
114+
l := sort.SearchInts(nums, target)
115+
r := sort.SearchInts(nums, target+1)
113116
return r - l
114117
}
115118
```
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
func search(nums []int, target int) int {
2-
l := sort.Search(len(nums), func(i int) bool { return nums[i] >= target })
3-
r := sort.Search(len(nums), func(i int) bool { return nums[i] > target })
2+
l := sort.SearchInts(nums, target)
3+
r := sort.SearchInts(nums, target+1)
44
return r - l
55
}

lcof/面试题53 - I. 在排序数组中查找数字 I/Solution.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
class Solution {
2+
private int[] nums;
3+
24
public int search(int[] nums, int target) {
3-
int l = lowerBound(nums, target);
4-
int r = lowerBound(nums, target + 1);
5+
this.nums = nums;
6+
int l = search(target);
7+
int r = search(target + 1);
58
return r - l;
69
}
710

8-
private int lowerBound(int[] nums, int x) {
11+
private int search(int x) {
912
int l = 0, r = nums.length;
1013
while (l < r) {
1114
int mid = (l + r) >>> 1;

lcof/面试题53 - II. 0~n-1中缺失的数字/README.md

+1-26
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcof/%E9%9D%A2%E8%AF%95%E9
4646

4747
最后返回左边界 $l$ 即可。
4848

49-
时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。其中 $n$ 是数组的长度
49+
时间复杂度 $O(\log n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
5050

5151
<!-- tabs:start -->
5252

@@ -185,29 +185,4 @@ public class Solution {
185185

186186
<!-- solution:end -->
187187

188-
<!-- solution:start-->
189-
190-
### 方法二
191-
192-
<!-- tabs:start -->
193-
194-
#### Rust
195-
196-
```rust
197-
impl Solution {
198-
pub fn missing_number(nums: Vec<i32>) -> i32 {
199-
let n = nums.len() as i32;
200-
let mut sum = ((1 + n) * n) / 2;
201-
for num in nums.iter() {
202-
sum -= num;
203-
}
204-
sum
205-
}
206-
}
207-
```
208-
209-
<!-- tabs:end -->
210-
211-
<!-- solution:end -->
212-
213188
<!-- problem:end -->

lcof/面试题53 - II. 0~n-1中缺失的数字/Solution2.rs

-10
This file was deleted.

lcof/面试题54. 二叉搜索树的第k大节点/README.md

-45
Original file line numberDiff line numberDiff line change
@@ -335,49 +335,4 @@ public class Solution {
335335

336336
<!-- solution:end -->
337337

338-
<!-- solution:start-->
339-
340-
### 方法二
341-
342-
<!-- tabs:start -->
343-
344-
#### Go
345-
346-
```go
347-
/**
348-
* Definition for a binary tree node.
349-
* type TreeNode struct {
350-
* Val int
351-
* Left *TreeNode
352-
* Right *TreeNode
353-
* }
354-
*/
355-
func kthLargest(root *TreeNode, k int) int {
356-
ch := make(chan int)
357-
ctx, cancel := context.WithCancel(context.Background())
358-
defer cancel()
359-
go inorder(ctx, root, ch)
360-
for ; k > 1; k-- {
361-
<-ch
362-
}
363-
return <-ch
364-
}
365-
366-
func inorder(ctx context.Context, cur *TreeNode, ch chan<- int) {
367-
if cur != nil {
368-
inorder(ctx, cur.Right, ch)
369-
select {
370-
case ch <- cur.Val:
371-
case <-ctx.Done():
372-
return
373-
}
374-
inorder(ctx, cur.Left, ch)
375-
}
376-
}
377-
```
378-
379-
<!-- tabs:end -->
380-
381-
<!-- solution:end -->
382-
383338
<!-- problem:end -->

lcof/面试题54. 二叉搜索树的第k大节点/Solution2.go

-30
This file was deleted.

lcof/面试题55 - I. 二叉树的深度/README.md

-32
Original file line numberDiff line numberDiff line change
@@ -222,36 +222,4 @@ public class Solution {
222222

223223
<!-- solution:end -->
224224

225-
<!-- solution:start-->
226-
227-
### 方法二
228-
229-
<!-- tabs:start -->
230-
231-
#### Python3
232-
233-
```python
234-
# Definition for a binary tree node.
235-
# class TreeNode:
236-
# def __init__(self, x):
237-
# self.val = x
238-
# self.left = None
239-
# self.right = None
240-
241-
242-
class Solution:
243-
def maxDepth(self, root: TreeNode) -> int:
244-
def dfs(root):
245-
if root is None:
246-
return 0
247-
l, r = dfs(root.left), dfs(root.right)
248-
return 1 + max(l, r)
249-
250-
return dfs(root)
251-
```
252-
253-
<!-- tabs:end -->
254-
255-
<!-- solution:end -->
256-
257225
<!-- problem:end -->

lcof/面试题55 - I. 二叉树的深度/Solution2.py

-17
This file was deleted.

lcof/面试题55 - II. 平衡二叉树/README.md

-36
Original file line numberDiff line numberDiff line change
@@ -313,40 +313,4 @@ public class Solution {
313313

314314
<!-- solution:end -->
315315

316-
<!-- solution:start-->
317-
318-
### 方法二
319-
320-
<!-- tabs:start -->
321-
322-
#### Python3
323-
324-
```python
325-
# Definition for a binary tree node.
326-
# class TreeNode:
327-
# def __init__(self, x):
328-
# self.val = x
329-
# self.left = None
330-
# self.right = None
331-
332-
333-
class Solution:
334-
def isBalanced(self, root: TreeNode) -> bool:
335-
def dfs(root):
336-
if root is None:
337-
return (True, 0)
338-
l, ld = dfs(root.left)
339-
r, rd = dfs(root.right)
340-
d = max(ld, rd) + 1
341-
if l and r and abs(ld - rd) <= 1:
342-
return (True, d)
343-
return (False, d)
344-
345-
return dfs(root)[0]
346-
```
347-
348-
<!-- tabs:end -->
349-
350-
<!-- solution:end -->
351-
352316
<!-- problem:end -->

lcof/面试题55 - II. 平衡二叉树/Solution2.py

-21
This file was deleted.

lcof/面试题56 - I. 数组中数字出现的次数/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcof/%E9%9D%A2%E8%AF%95%E9
5151

5252
对两个组分别进行异或运算,即可得到两个只出现一次的数字。
5353

54-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度
54+
时间复杂度 $O(n)$,其中 $n$ 为数组长度。空间复杂度 $O(1)$。
5555

5656
<!-- tabs:start -->
5757

lcof/面试题56 - II. 数组中数字出现的次数 II/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcof/%E9%9D%A2%E8%AF%95%E9
4848

4949
我们用一个长度为 32 的数组 $cnt$ 来统计所有数字的每一位中 $1$ 的出现次数。如果某一位的 $1$ 的出现次数能被 $3$ 整除,那么那个只出现一次的数字二进制表示中对应的那一位也是 $0$;否则,那个只出现一次的数字二进制表示中对应的那一位是 $1$。
5050

51-
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 是数组的长度;而 $C$ 是整数的位数,本题中 $C=32$。
51+
时间复杂度 $O(n \times C)$,空间复杂度 $O(C)$。其中 $n$ 是数组的长度;而 $C$ 是整数的位数,本题中 $C=32$。
5252

5353
<!-- tabs:start -->
5454

0 commit comments

Comments
 (0)