Skip to content

Commit a45ea87

Browse files
committed
Merge branch 'main' of github.com:ldtech007/leetcode
2 parents f8e659c + 053230f commit a45ea87

11 files changed

+773
-5
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@
3939
|[leetcode 242.有效的字母异位词](./leetcode%20blind75/字符串/LeetCode%200242.%20有效的字母异位词.md)| ✔ | ✔ | ✔ | [有效的字母异位词](https://www.bilibili.com/video/BV1db421J7qK/) |
4040
|[leetcode 424.替换后的最长重复字符](./leetcode%20blind75/字符串/LeetCode%200424.%20替换后的最长重复字符.md)| ✔ | ✔ | ✔ | [替换后的最长重复字符](https://www.bilibili.com/video/BV1zf421d7Ui/) |
4141
|[leetcode 647.回文子串](./leetcode%20blind75/字符串/LeetCode%200647.%20回文子串.md)| ✔ | ✔ | ✔ | [回文子串](https://www.bilibili.com/video/BV1Bs421w7iw/) |
42-
|[leetcode 19.删除链表的倒数第N个结点](./leetcode%20blind75/链表/LeetCode%200019.%20删除链表的倒数第%20N%20个结点.md)| ✔ | ✔ | ✔ | 录制中 |
43-
|[leetcode 21.合并两个有序链表](./leetcode%20blind75/链表/LeetCode%200021.%20%E5%90%88%E5%B9%B6%E4%B8%A4%E4%B8%AA%E6%9C%89%E5%BA%8F%E9%93%BE%E8%A1%A8.md)| ✔ | ✘ | ✘ | 录制中 |
44-
|[leetcode 23.合并 K 个升序链表](./leetcode%20blind75/链表/LeetCode%200023.%20%E5%90%88%E5%B9%B6%20K%20%E4%B8%AA%E5%8D%87%E5%BA%8F%E9%93%BE%E8%A1%A8.md)| ✔ | ✘ | ✘ | 录制中 |
45-
|[leetcode 141.环形链表](./leetcode%20blind75/链表/LeetCode%200141.%E7%8E%AF%E5%BD%A2%E9%93%BE%E8%A1%A8.md)| ✔ | ✘ | ✘ | 录制中 |
46-
|[leetcode 143.重排链表](./leetcode%20blind75/链表/LeetCode%200143.%20%E9%87%8D%E6%8E%92%E9%93%BE%E8%A1%A8.md)| ✔ | ✘ | ✘ | 录制中 |
42+
|[leetcode 19.删除链表的倒数第N个结点](./leetcode%20blind75/链表/LeetCode%200019.%20删除链表的倒数第%20N%20个结点.md)| ✔ | ✔ | ✔ | [删除链表的倒数第N个结点](https://www.bilibili.com/video/BV1hs421M7Ke/) |
43+
|[leetcode 21.合并两个有序链表](./leetcode%20blind75/链表/LeetCode%200021.%20%E5%90%88%E5%B9%B6%E4%B8%A4%E4%B8%AA%E6%9C%89%E5%BA%8F%E9%93%BE%E8%A1%A8.md)| ✔ | ✔ | ✔ | [合并两个有序链表](https://www.bilibili.com/video/BV17w4m1Y7CJ/) |
44+
|[leetcode 23.合并 K 个升序链表](./leetcode%20blind75/链表/LeetCode%200023.%20%E5%90%88%E5%B9%B6%20K%20%E4%B8%AA%E5%8D%87%E5%BA%8F%E9%93%BE%E8%A1%A8.md)| ✔ | ✔ | ✔ | [合并 K 个升序链表](https://www.bilibili.com/video/BV136421f7hR/)] |
45+
|[leetcode 141.环形链表](./leetcode%20blind75/链表/LeetCode%200141.%E7%8E%AF%E5%BD%A2%E9%93%BE%E8%A1%A8.md)| ✔ | ✔ | ✔ | [环形链表](https://www.bilibili.com/video/BV18z421q78k/) |
46+
|[leetcode 143.重排链表](./leetcode%20blind75/链表/LeetCode%200143.%20%E9%87%8D%E6%8E%92%E9%93%BE%E8%A1%A8.md)| ✔ | ✔ | ✔ | [重排链表](https://www.bilibili.com/video/BV1tx4y1t7YB/) |
4747
|[leetcode 206.反转链表](./leetcode%20blind75/链表/LeetCode%200206.%20%E5%8F%8D%E8%BD%AC%E9%93%BE%E8%A1%A8.md)| ✔ | ✔ | ✔ | [反转链表](https://www.bilibili.com/video/BV1gE421N7W2/) |
48+
|[leetcode 190.颠倒二进制位](./leetcode%20blind75/%E4%BD%8D%E8%BF%90%E7%AE%97/LeetCode%200190.%E9%A2%A0%E5%80%92%E4%BA%8C%E8%BF%9B%E5%88%B6%E4%BD%8D.md)| ✔ | ✘ | ✘ | 录制中 |
4849

4950
所有的文章都是我利用工作之余的时间写的,尽可能使用最简洁易懂的方式,由浅入深,还画了一些帮助理解的图片。每写完一篇我都会以小白的姿态去反复阅读,反复修改。 也获得了一些同学的认可,还有一些同学在力扣的题解评论区给我提建议,我也会根据同学们的建议去不断调整。我始终认为个人的思路是有局限性的,因为每个人对基础知识的掌握程度是不一样的,一个人认为理所当然的结论,另一个人可能想破脑袋都不理解,所以需要我们共同去不断完善。非常欢迎大家通过力扣评论、公众号「编程网事」后台给我提建议,或者直接提给我你的合并请求。
5051

leetcode blind75/位运算/LeetCode 0190.颠倒二进制位.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,41 @@ public:
6262
}
6363
};
6464
```
65+
66+
### java代码
67+
68+
```java
69+
public class Solution {
70+
// you need treat n as an unsigned value
71+
public int reverseBits(int n) {
72+
int res = 0;
73+
for (int i = 0; i < 32; ++i) {
74+
// 原串的第i位
75+
int bit = n & 1;
76+
// 将原串的第i位放到res的第31-i位
77+
res |= (bit << (31 - i));
78+
n >>= 1;
79+
}
80+
return res;
81+
}
82+
}
83+
```
84+
85+
### python代码
86+
87+
```python
88+
class Solution:
89+
def reverseBits(self, n: int) -> int:
90+
res = 0
91+
for i in range(32):
92+
# 原串的第i位
93+
bit = n & 1
94+
# 将原串的第i位放到res的第31-i位
95+
res |= (bit << (31 - i))
96+
n >>= 1
97+
return res
98+
```
99+
65100
### 方法二 分治
66101

67102
下面介绍另一种分治方法,对一些刚刷题的同学来说可能比较难理解。
@@ -106,6 +141,46 @@ public:
106141
}
107142
};
108143
```
144+
145+
### java代码
146+
147+
```java
148+
public class Solution {
149+
// you need treat n as an unsigned value
150+
public int reverseBits(int n) {
151+
// 左右16位交换
152+
n = ((n >> 16) & 0x0000ffff) | ((n << 16) & 0xffff0000);
153+
// 左右8位交换
154+
n = ((n >> 8) & 0x00ff00ff) | ((n << 8) & 0xff00ff00);
155+
// 左右4位交换
156+
n = ((n >> 4) & 0x0f0f0f0f) | ((n << 4) & 0xf0f0f0f0);
157+
// 左右2位交换
158+
n = ((n >> 2) & 0x33333333) | ((n << 2) & 0xcccccccc);
159+
// 左右1位交换
160+
n = ((n >> 1) & 0x55555555) | ((n << 1) & 0xaaaaaaaa);
161+
return n;
162+
}
163+
}
164+
```
165+
166+
### python代码
167+
168+
```python
169+
class Solution:
170+
def reverseBits(self, n: int) -> int:
171+
# 左右16位交换
172+
n = (n >> 16) | (n << 16)
173+
# 左右8位交换
174+
n = ((n & 0xff00ff00) >> 8) | ((n & 0x00ff00ff) << 8)
175+
# 左右4位交换
176+
n = ((n & 0xf0f0f0f0) >> 4) | ((n & 0x0f0f0f0f) << 4)
177+
# 左右2位交换
178+
n = ((n & 0xcccccccc) >> 2) | ((n & 0x33333333) << 2)
179+
# 左右1位交换
180+
n = ((n & 0xaaaaaaaa) >> 1) | ((n & 0x55555555) << 1)
181+
return n
182+
```
183+
109184
## 复杂度分析
110185

111186
**时间复杂度:** 两种方法都是 *O(1)*,最多处理`32`位,分治是`log32`位,都是常量时间。

leetcode blind75/位运算/LeetCode 0191.位1的个数.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,41 @@ public:
4848
}
4949
};
5050
```
51+
52+
### java代码
53+
54+
```java
55+
class Solution {
56+
public int hammingWeight(int n) {
57+
int res = 0;
58+
while (n != 0) {
59+
// 获取n对应二进制最右边一位
60+
if (n % 2 == 1) {
61+
res++;
62+
}
63+
// n对应的二进制右移一位
64+
n = n / 2;
65+
}
66+
return res;
67+
}
68+
}
69+
```
70+
71+
### python代码
72+
73+
```python
74+
class Solution:
75+
def hammingWeight(self, n: int) -> int:
76+
res = 0
77+
while n:
78+
# 获取n对应二进制最右边一位
79+
if n % 2:
80+
res += 1
81+
# n对应的二进制右移一位
82+
n = n // 2
83+
return res
84+
```
85+
5186
### 方法二
5287

5388
定义`res`保存`1`的个数。对于无符号整数`n`,统计其中`1`的个数步骤如下:
@@ -77,6 +112,35 @@ public:
77112
};
78113
```
79114
115+
### java代码
116+
117+
```java
118+
class Solution {
119+
public int hammingWeight(int n) {
120+
int res = 0;
121+
while (n != 0) {
122+
res++;
123+
// n对应二进制最右边不为零的bit位置为零
124+
n = n & (n - 1);
125+
}
126+
return res;
127+
}
128+
}
129+
```
130+
131+
### python代码
132+
133+
```python
134+
class Solution:
135+
def hammingWeight(self, n: int) -> int:
136+
res = 0
137+
while n:
138+
res += 1
139+
# n对应二进制最右边不为零的bit位置为零
140+
n = n & (n - 1)
141+
return res
142+
```
143+
80144
## 复杂度分析
81145

82146
**时间复杂度:** 两种方法都是*O(1)*,因为二进制最长为`32`位。

leetcode blind75/位运算/LeetCode 0268.丢失的数字.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,36 @@ public:
4747
}
4848
};
4949
```
50+
51+
### java代码
52+
53+
```java
54+
class Solution {
55+
public int missingNumber(int[] nums) {
56+
int nums_len = nums.length;
57+
int res = nums_len;
58+
for (int i = 0; i < nums_len; ++i) {
59+
//[0,n]和nums中的元素做异或操作
60+
res ^= (i ^ nums[i]);
61+
}
62+
return res;
63+
}
64+
}
65+
```
66+
67+
### python 代码
68+
69+
```python
70+
class Solution:
71+
def missingNumber(self, nums: List[int]) -> int:
72+
nums_len = len(nums)
73+
res = nums_len
74+
for i in range(nums_len):
75+
#[0,n]和nums中的元素做异或操作
76+
res ^= (i ^ nums[i])
77+
return res
78+
```
79+
5080
### 方法二 数学运算
5181

5282
因为区间`[0, n]`上有`n + 1`个元素,数组`nums`中只有`n`个元素,假设缺失的元素为`X`,我们可以得到如下公式:
@@ -73,6 +103,35 @@ public:
73103
};
74104
```
75105
106+
### java代码
107+
108+
```java
109+
class Solution {
110+
public int missingNumber(int[] nums) {
111+
int nums_len = nums.length;
112+
int res = nums_len;
113+
for (int i = 0; i < nums_len; ++i) {
114+
//[0,n]的和减去nums中所有元素的和
115+
res += (i - nums[i]);
116+
}
117+
return res;
118+
}
119+
}
120+
```
121+
122+
### python代码
123+
124+
```python
125+
class Solution:
126+
def missingNumber(self, nums: List[int]) -> int:
127+
nums_len = len(nums)
128+
res = nums_len
129+
for i in range(nums_len):
130+
#[0,n]的和减去nums中所有元素的和
131+
res += (i - nums[i])
132+
return res
133+
```
134+
76135
## 复杂度分析
77136

78137
**时间复杂度:** 两种方法的整个过程都是只遍历了一遍数组,所以时间复杂度为*O(n)*`n`为数组`nums`的长度。

leetcode blind75/位运算/LeetCode 0338. 比特位计数.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,42 @@ public:
9090
};
9191
```
9292
93+
### java代码
94+
95+
```java
96+
class Solution {
97+
public int[] countBits(int n) {
98+
int[] dp = new int[n + 1];
99+
for (int i = 1; i <= n; ++i) {
100+
if (i % 2 == 0) {
101+
// i为偶数
102+
dp[i] = dp[i / 2];
103+
} else {
104+
// i为奇数
105+
dp[i] = dp[i / 2] + 1;
106+
}
107+
}
108+
return dp;
109+
}
110+
}
111+
```
112+
113+
### python代码
114+
115+
```python
116+
class Solution:
117+
def countBits(self, n: int) -> List[int]:
118+
dp = [0] * (n + 1)
119+
for i in range(1, n + 1):
120+
if i % 2 == 0:
121+
# i为偶数
122+
dp[i] = dp[i // 2]
123+
else:
124+
# i为奇数
125+
dp[i] = dp[i // 2] + 1
126+
return dp
127+
```
128+
93129
### 方法二
94130

95131
我们先介绍一个骚操作,对于一个整数`n``n & (n - 1)`可以将`n`的二进制最右边值为`1``bit位`置为`0`
@@ -121,7 +157,32 @@ public:
121157
}
122158
};
123159
```
160+
### java代码
161+
162+
```java
163+
class Solution {
164+
public int[] countBits(int n) {
165+
int[] dp = new int[n + 1];
166+
for (int i = 1; i <= n; ++i) {
167+
// 状态转移公式
168+
dp[i] = dp[i & (i - 1)] + 1;
169+
}
170+
return dp;
171+
}
172+
}
173+
```
124174

175+
### python 代码
176+
177+
```python
178+
class Solution:
179+
def countBits(self, n: int) -> List[int]:
180+
dp = [0] * (n + 1)
181+
for i in range(1, n + 1):
182+
# 状态转移公式
183+
dp[i] = dp[i & (i - 1)] + 1
184+
return dp
185+
```
125186

126187
## 复杂度分析
127188

leetcode blind75/位运算/LeetCode 0371.两整数之和.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,37 @@ public:
9292
};
9393
```
9494

95+
## java代码
96+
97+
```java
98+
class Solution {
99+
public int getSum(int a, int b) {
100+
while (b != 0) {
101+
int temp_add = a ^ b; // 不包含进位a、b每位的和
102+
// a、b每位和的进位,左移有符号值会溢出,赋给无符号的会自动取模
103+
int temp_carry = (a & b) << 1;
104+
a = temp_add; // a、b每位的和赋值给a
105+
b = temp_carry; // a、b每位的进位赋值给b
106+
}
107+
return a;
108+
}
109+
}
110+
```
111+
112+
## python代码
113+
114+
```python
115+
class Solution:
116+
def getSum(self, a: int, b: int) -> int:
117+
while b:
118+
temp_add = a ^ b # 不包含进位a、b每位的和
119+
# a、b每位和的进位,左移有符号值会溢出,赋给无符号的会自动取模
120+
temp_carry = (a & b) << 1
121+
a = temp_add # a、b每位的和赋值给a
122+
b = temp_carry # a、b每位的进位赋值给b
123+
return a
124+
```
125+
95126
## 复杂度分析
96127

97128
**时间复杂度:** *O(1)*,因为整数在`32`位操作系统上占`32`位,最多计算`32``temp_carry`,故为常数的时间复杂度。

leetcode blind75/链表/LeetCode 0019. 删除链表的倒数第 N 个结点.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
> *题目链接:* https://leetcode.cn/problems/remove-nth-node-from-end-of-list/
2+
>
3+
>*视频题解:* https://www.bilibili.com/video/BV1hs421M7Ke/
24
35
# LeetCode 19. 删除链表的倒数第 N 个结点
46

0 commit comments

Comments
 (0)