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 44a6967

Browse files
feat: update solutions to lcof problems (doocs#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‎

Lines changed: 9 additions & 6 deletions
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
```
Lines changed: 2 additions & 2 deletions
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(iint) bool { returnnums[i] >=target })
3-
r := sort.Search(len(nums), func(iint) bool { returnnums[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‎

Lines changed: 6 additions & 3 deletions
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‎

Lines changed: 1 addition & 26 deletions
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‎

Lines changed: 0 additions & 10 deletions
This file was deleted.

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

Lines changed: 0 additions & 45 deletions
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‎

Lines changed: 0 additions & 30 deletions
This file was deleted.

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

Lines changed: 0 additions & 32 deletions
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‎

Lines changed: 0 additions & 17 deletions
This file was deleted.

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

Lines changed: 0 additions & 36 deletions
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 -->

0 commit comments

Comments
(0)

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