Skip to content

Commit d3bfbbe

Browse files
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
2 parents a79bf97 + c342d7e commit d3bfbbe

File tree

111 files changed

+3302
-333
lines changed

Some content is hidden

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

111 files changed

+3302
-333
lines changed

problems/0001.两数之和.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public:
133133
### Java:
134134
135135
```java
136+
//使用哈希表
136137
public int[] twoSum(int[] nums, int target) {
137138
int[] res = new int[2];
138139
if(nums == null || nums.length == 0){
@@ -151,6 +152,43 @@ public int[] twoSum(int[] nums, int target) {
151152
return res;
152153
}
153154
```
155+
```java
156+
//使用双指针
157+
public int[] twoSum(int[] nums, int target) {
158+
int m=0,n=0,k,board=0;
159+
int[] res=new int[2];
160+
int[] tmp1=new int[nums.length];
161+
//备份原本下标的nums数组
162+
System.arraycopy(nums,0,tmp1,0,nums.length);
163+
//将nums排序
164+
Arrays.sort(nums);
165+
//双指针
166+
for(int i=0,j=nums.length-1;i<j;){
167+
if(nums[i]+nums[j]<target)
168+
i++;
169+
else if(nums[i]+nums[j]>target)
170+
j--;
171+
else if(nums[i]+nums[j]==target){
172+
m=i;
173+
n=j;
174+
break;
175+
}
176+
}
177+
//找到nums[m]在tmp1数组中的下标
178+
for(k=0;k<nums.length;k++){
179+
if(tmp1[k]==nums[m]){
180+
res[0]=k;
181+
break;
182+
}
183+
}
184+
//找到nums[n]在tmp1数组中的下标
185+
for(int i=0;i<nums.length;i++){
186+
if(tmp1[i]==nums[n]&&i!=k)
187+
res[1]=i;
188+
}
189+
return res;
190+
}
191+
```
154192

155193
### Python:
156194
(版本一) 使用字典

problems/0005.最长回文子串.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ char * longestPalindrome(char * s){
618618
### C#:
619619

620620
動態規則:
621-
```c#
621+
```csharp
622622
public class Solution {
623623

624624
public string LongestPalindrome(string s) {
@@ -648,7 +648,7 @@ public class Solution {
648648
```
649649

650650
雙指針:
651-
```C#
651+
```csharp
652652
public class Solution {
653653
int maxlenth = 0;
654654
int left = 0;

problems/0015.三数之和.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ while (right > left) {
256256
## 其他语言版本
257257

258258
### Java:
259-
259+
(版本一) 双指针
260260
```Java
261261
class Solution {
262262
public List<List<Integer>> threeSum(int[] nums) {
@@ -297,7 +297,43 @@ class Solution {
297297
}
298298
}
299299
```
300+
(版本二) 使用哈希集合
301+
```Java
302+
class Solution {
303+
public List<List<Integer>> threeSum(int[] nums) {
304+
List<List<Integer>> result = new ArrayList<>();
305+
Arrays.sort(nums);
300306

307+
for (int i = 0; i < nums.length; i++) {
308+
// 如果第一个元素大于零,不可能凑成三元组
309+
if (nums[i] > 0) {
310+
return result;
311+
}
312+
// 三元组元素a去重
313+
if (i > 0 && nums[i] == nums[i - 1]) {
314+
continue;
315+
}
316+
317+
HashSet<Integer> set = new HashSet<>();
318+
for (int j = i + 1; j < nums.length; j++) {
319+
// 三元组元素b去重
320+
if (j > i + 2 && nums[j] == nums[j - 1] && nums[j - 1] == nums[j - 2]) {
321+
continue;
322+
}
323+
324+
int c = -nums[i] - nums[j];
325+
if (set.contains(c)) {
326+
result.add(Arrays.asList(nums[i], nums[j], c));
327+
set.remove(c); // 三元组元素c去重
328+
} else {
329+
set.add(nums[j]);
330+
}
331+
}
332+
}
333+
return result;
334+
}
335+
}
336+
```
301337
### Python:
302338
(版本一) 双指针
303339

problems/0017.电话号码的字母组合.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ def backtracking(result, letter_map, digits, path, index)
733733
end
734734
```
735735
### C#
736-
```C#
736+
```csharp
737737
public class Solution
738738
{
739739
public IList<string> res = new List<string>();

problems/0019.删除链表的倒数第N个节点.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public:
8282

8383
// ListNode *tmp = slow->next; C++释放内存的逻辑
8484
// slow->next = tmp->next;
85-
// delete nth;
85+
// delete tmp;
8686

8787
return dummyHead->next;
8888
}

problems/0024.两两交换链表中的节点.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ impl Solution {
462462
```
463463

464464
### C#
465-
```C#
465+
```csharp
466466
// 虚拟头结点
467467
public ListNode SwapPairs(ListNode head)
468468
{

problems/0028.实现strStr.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,8 @@ public:
425425
if (needle.size() == 0) {
426426
return 0;
427427
}
428-
int next[needle.size()];
429-
getNext(next, needle);
428+
vector<int> next(needle.size());
429+
getNext(&next[0], needle);
430430
int j = -1; // // 因为next数组里记录的起始位置为-1
431431
for (int i = 0; i < haystack.size(); i++) { // 注意i就从0开始
432432
while(j >= 0 && haystack[i] != needle[j + 1]) { // 不匹配
@@ -524,8 +524,8 @@ public:
524524
if (needle.size() == 0) {
525525
return 0;
526526
}
527-
int next[needle.size()];
528-
getNext(next, needle);
527+
vector<int> next(needle.size());
528+
getNext(&next[0], needle);
529529
int j = 0;
530530
for (int i = 0; i < haystack.size(); i++) {
531531
while(j > 0 && haystack[i] != needle[j]) {
@@ -1359,7 +1359,7 @@ impl Solution {
13591359
```
13601360

13611361
>前缀表统一不减一
1362-
```C#
1362+
```csharp
13631363
public int StrStr(string haystack, string needle)
13641364
{
13651365
if (string.IsNullOrEmpty(needle))
@@ -1428,4 +1428,3 @@ public int[] GetNext(string needle)
14281428
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
14291429
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
14301430
</a>
1431-

problems/0034.在排序数组中查找元素的第一个和最后一个位置.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ class Solution {
331331

332332
### C#
333333

334-
```c#
334+
```csharp
335335
public int[] SearchRange(int[] nums, int target) {
336336

337337
var leftBorder = GetLeftBorder(nums, target);

problems/0045.跳跃游戏II.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,34 @@ class Solution:
285285

286286
### Go
287287

288+
289+
```go
290+
/**
291+
* @date: 2024 Jan 06
292+
* @time: 13:44
293+
* @author: Chris
294+
**/
295+
// 贪心算法优化版
296+
297+
// 记录步骤规则:每超过上一次可达最大范围,需要跳跃一次,次数+1
298+
// 记录位置:i == lastDistance + 1
299+
func jump(nums []int) int {
300+
// 根据题目规则,初始位置为nums[0]
301+
lastDistance := 0 // 上一次覆盖范围
302+
curDistance := 0 // 当前覆盖范围(可达最大范围)
303+
minStep := 0 // 记录最少跳跃次数
304+
305+
for i := 0; i < len(nums); i++ {
306+
if i == lastDistance+1 { // 在上一次可达范围+1的位置,记录步骤
307+
minStep++ // 跳跃次数+1
308+
lastDistance = curDistance // 记录时才可以更新
309+
}
310+
curDistance = max(nums[i]+i, curDistance) // 更新当前可达的最大范围
311+
}
312+
return minStep
313+
}
314+
```
315+
288316
```go
289317
// 贪心版本一
290318
func jump(nums []int) int {
@@ -464,7 +492,34 @@ impl Solution {
464492
}
465493
}
466494
```
495+
### C
496+
497+
```c
498+
#define max(a, b) ((a) > (b) ? (a) : (b))
499+
500+
int jump(int* nums, int numsSize) {
501+
if(numsSize == 1){
502+
return 0;
503+
}
504+
int count = 0;
505+
// 记录当前能走的最远距离
506+
int curDistance = 0;
507+
// 记录下一步能走的最远距离
508+
int nextDistance = 0;
509+
for(int i = 0; i < numsSize; i++){
510+
nextDistance = max(i + nums[i], nextDistance);
511+
// 下标到了当前的最大距离
512+
if(i == nextDistance){
513+
count++;
514+
curDistance = nextDistance;
515+
}
516+
}
517+
return count;
518+
}
519+
```
520+
467521
### C#
522+
468523
```csharp
469524
// 版本二
470525
public class Solution
@@ -490,3 +545,4 @@ public class Solution
490545
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
491546
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
492547
</a>
548+

problems/0053.最大子序和.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,25 @@ class Solution:
230230

231231
```
232232
### Go
233+
贪心法
234+
```go
235+
func maxSubArray(nums []int) int {
236+
max := nums[0]
237+
count := 0
233238

239+
for i := 0; i < len(nums); i++{
240+
count += nums[i]
241+
if count > max{
242+
max = count
243+
}
244+
if count < 0 {
245+
count = 0
246+
}
247+
}
248+
return max
249+
}
250+
```
251+
动态规划
234252
```go
235253
func maxSubArray(nums []int) int {
236254
maxSum := nums[0]

0 commit comments

Comments
 (0)