Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update solutions to lcof problems #2908

Merged
merged 3 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: update solutions to lcof problems
  • Loading branch information
yanglbme committed May 24, 2024
commit 658449f92a37f65c83440ba207b16899cb84d1bc
15 changes: 9 additions & 6 deletions lcof/面试题53 - I. 在排序数组中查找数字 I/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcof/%E9%9D%A2%E8%AF%95%E9

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

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

<!-- tabs:start -->

Expand All @@ -70,13 +70,16 @@ class Solution:

```java
class Solution {
private int[] nums;

public int search(int[] nums, int target) {
int l = lowerBound(nums, target);
int r = lowerBound(nums, target + 1);
this.nums = nums;
int l = search(target);
int r = search(target + 1);
return r - l;
}

private int lowerBound(int[] nums, int x) {
private int search(int x) {
int l = 0, r = nums.length;
while (l < r) {
int mid = (l + r) >>> 1;
Expand Down Expand Up @@ -108,8 +111,8 @@ public:

```go
func search(nums []int, target int) int {
l := sort.Search(len(nums), func(i int) bool { return nums[i] >= target })
r := sort.Search(len(nums), func(i int) bool { return nums[i] > target })
l := sort.SearchInts(nums, target)
r := sort.SearchInts(nums, target+1)
return r - l
}
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
func search(nums []int, target int) int {
l := sort.Search(len(nums), func(i int) bool { return nums[i] >= target })
r := sort.Search(len(nums), func(i int) bool { return nums[i] > target })
l := sort.SearchInts(nums, target)
r := sort.SearchInts(nums, target+1)
return r - l
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
class Solution {
private int[] nums;

public int search(int[] nums, int target) {
int l = lowerBound(nums, target);
int r = lowerBound(nums, target + 1);
this.nums = nums;
int l = search(target);
int r = search(target + 1);
return r - l;
}

private int lowerBound(int[] nums, int x) {
private int search(int x) {
int l = 0, r = nums.length;
while (l < r) {
int mid = (l + r) >>> 1;
Expand Down
27 changes: 1 addition & 26 deletions lcof/面试题53 - II. 0~n-1中缺失的数字/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcof/%E9%9D%A2%E8%AF%95%E9

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

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

<!-- tabs:start -->

Expand Down Expand Up @@ -185,29 +185,4 @@ public class Solution {

<!-- solution:end -->

<!-- solution:start-->

### 方法二

<!-- tabs:start -->

#### Rust

```rust
impl Solution {
pub fn missing_number(nums: Vec<i32>) -> i32 {
let n = nums.len() as i32;
let mut sum = ((1 + n) * n) / 2;
for num in nums.iter() {
sum -= num;
}
sum
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
10 changes: 0 additions & 10 deletions lcof/面试题53 - II. 0~n-1中缺失的数字/Solution2.rs

This file was deleted.

45 changes: 0 additions & 45 deletions lcof/面试题54. 二叉搜索树的第k大节点/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,49 +335,4 @@ public class Solution {

<!-- solution:end -->

<!-- solution:start-->

### 方法二

<!-- tabs:start -->

#### Go

```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func kthLargest(root *TreeNode, k int) int {
ch := make(chan int)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go inorder(ctx, root, ch)
for ; k > 1; k-- {
<-ch
}
return <-ch
}

func inorder(ctx context.Context, cur *TreeNode, ch chan<- int) {
if cur != nil {
inorder(ctx, cur.Right, ch)
select {
case ch <- cur.Val:
case <-ctx.Done():
return
}
inorder(ctx, cur.Left, ch)
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
30 changes: 0 additions & 30 deletions lcof/面试题54. 二叉搜索树的第k大节点/Solution2.go

This file was deleted.

32 changes: 0 additions & 32 deletions lcof/面试题55 - I. 二叉树的深度/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,36 +222,4 @@ public class Solution {

<!-- solution:end -->

<!-- solution:start-->

### 方法二

<!-- tabs:start -->

#### Python3

```python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None


class Solution:
def maxDepth(self, root: TreeNode) -> int:
def dfs(root):
if root is None:
return 0
l, r = dfs(root.left), dfs(root.right)
return 1 + max(l, r)

return dfs(root)
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
17 changes: 0 additions & 17 deletions lcof/面试题55 - I. 二叉树的深度/Solution2.py

This file was deleted.

36 changes: 0 additions & 36 deletions lcof/面试题55 - II. 平衡二叉树/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,40 +313,4 @@ public class Solution {

<!-- solution:end -->

<!-- solution:start-->

### 方法二

<!-- tabs:start -->

#### Python3

```python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None


class Solution:
def isBalanced(self, root: TreeNode) -> bool:
def dfs(root):
if root is None:
return (True, 0)
l, ld = dfs(root.left)
r, rd = dfs(root.right)
d = max(ld, rd) + 1
if l and r and abs(ld - rd) <= 1:
return (True, d)
return (False, d)

return dfs(root)[0]
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
21 changes: 0 additions & 21 deletions lcof/面试题55 - II. 平衡二叉树/Solution2.py

This file was deleted.

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

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

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcof/%E9%9D%A2%E8%AF%95%E9

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

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

<!-- tabs:start -->

Expand Down
Loading
Loading