Skip to content

Commit b1a7952

Browse files
committed
bk
1 parent 677d415 commit b1a7952

File tree

120 files changed

+4116
-2615
lines changed

Some content is hidden

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

120 files changed

+4116
-2615
lines changed

0001.two-sum/two-sum.md

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
<p>Given an array of integers, return <strong>indices</strong> of the two numbers such that they add up to a specific target.</p>
2-
3-
<p>You may assume that each input would have <strong><em>exactly</em></strong> one solution, and you may not use the <em>same</em> element twice.</p>
4-
5-
<p><strong>Example:</strong></p>
6-
7-
<pre>
8-
Given nums = [2, 7, 11, 15], target = 9,
9-
10-
Because nums[<strong>0</strong>] + nums[<strong>1</strong>] = 2 + 7 = 9,
11-
return [<strong>0</strong>, <strong>1</strong>].
12-
</pre>
13-
14-
<p>&nbsp;</p>
1+
<p>Given an array of integers, return <strong>indices</strong> of the two numbers such that they add up to a specific target.</p>
2+
3+
<p>You may assume that each input would have <strong><em>exactly</em></strong> one solution, and you may not use the <em>same</em> element twice.</p>
4+
5+
<p><strong>Example:</strong></p>
6+
7+
<pre>
8+
Given nums = [2, 7, 11, 15], target = 9,
9+
10+
Because nums[<strong>0</strong>] + nums[<strong>1</strong>] = 2 + 7 = 9,
11+
return [<strong>0</strong>, <strong>1</strong>].
12+
</pre>
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,90 @@
1-
/*
2-
* @lc app=leetcode id=24 lang=cpp
3-
*
4-
* [24] Swap Nodes in Pairs
5-
*
6-
* https://leetcode.com/problems/swap-nodes-in-pairs/description/
7-
*
8-
* algorithms
9-
* Medium (43.65%)
10-
* Total Accepted: 294.1K
11-
* Total Submissions: 672.2K
12-
* Testcase Example: '[1,2,3,4]'
13-
*
14-
* Given a linked list, swap every two adjacent nodes and return its head.
15-
*
16-
* You may not modify the values in the list's nodes, only nodes itself may be
17-
* changed.
18-
*
19-
*
20-
*
21-
* Example:
22-
*
23-
*
24-
* Given 1->2->3->4, you should return the list as 2->1->4->3.
25-
*
26-
*
27-
*/
28-
/**
29-
* Definition for singly-linked list.
30-
* struct ListNode {
31-
* int val;
32-
* ListNode *next;
33-
* ListNode(int x) : val(x), next(NULL) {}
34-
* };
35-
*/
36-
class Solution {
37-
public:
38-
// 迭代,注意我们转移指针的时候不会跳跃。如果跳跃,我觉得应该是有问题,或者说没有写好。
39-
ListNode* swapPairs(ListNode* head) {
40-
ListNode* newhead=new ListNode(-100);
41-
newhead->next=head;
42-
ListNode *pre=newhead;
43-
while(pre){
44-
ListNode* f=pre->next;
45-
if(!f) return newhead->next;
46-
47-
ListNode* s=f->next;
48-
if(!s) return newhead->next;
49-
50-
pre->next=s;
51-
f->next=s->next;
52-
s->next=f;
53-
54-
pre=f;
55-
}
56-
return newhead->next;
57-
58-
59-
60-
61-
}
62-
63-
64-
65-
// 递归做法
66-
ListNode* swapPairs1(ListNode* head) {
67-
if(!head) return head;
68-
ListNode* s=head->next;
69-
if(!s) return head;
70-
if(!s->next){// 处理最后两个节点。
71-
head->next=nullptr;
72-
s->next=head;
73-
return s;
74-
}
75-
head->next=swapPairs(s->next);
76-
s->next=head;
77-
return s;
78-
}
79-
};
80-
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
12+
// 迭代版
13+
ListNode* swapPairs(ListNode* head) {
14+
ListNode* dummy=new ListNode(INT_MIN);
15+
dummy->next=head;
16+
17+
auto cur=dummy;
18+
while(cur){
19+
auto son=cur->next;
20+
if(!son) break;
21+
22+
auto grand=son->next;
23+
if(!grand) break;
24+
25+
cur->next=grand;
26+
son->next=grand->next;
27+
grand->next=son;
28+
29+
cur=son;
30+
}
31+
32+
return dummy->next;
33+
}
34+
35+
36+
// 递归版
37+
ListNode* swapPairs1(ListNode* f) {
38+
if(!f || !f->next) return f;
39+
auto s=f->next;
40+
auto tail=swapPairs(s->next);
41+
s->next=f;
42+
f->next=tail;
43+
return s;
44+
}
45+
46+
47+
/*
48+
// 迭代,注意我们转移指针的时候不会跳跃。如果跳跃,我觉得应该是有问题,或者说没有写好。
49+
ListNode* swapPairs(ListNode* head) {
50+
ListNode* newhead=new ListNode(-100);
51+
newhead->next=head;
52+
ListNode *pre=newhead;
53+
while(pre){
54+
ListNode* f=pre->next;
55+
if(!f) return newhead->next;
56+
57+
ListNode* s=f->next;
58+
if(!s) return newhead->next;
59+
60+
pre->next=s;
61+
f->next=s->next;
62+
s->next=f;
63+
64+
pre=f;
65+
}
66+
return newhead->next;
67+
68+
69+
70+
71+
}
72+
73+
74+
75+
// 递归做法
76+
ListNode* swapPairs1(ListNode* head) {
77+
if(!head) return head;
78+
ListNode* s=head->next;
79+
if(!s) return head;
80+
if(!s->next){// 处理最后两个节点。
81+
head->next=nullptr;
82+
s->next=head;
83+
return s;
84+
}
85+
head->next=swapPairs(s->next);
86+
s->next=head;
87+
return s;
88+
}
89+
*/
90+
};
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//https://leetcode.com/problems/wildcard-matching/discuss/17810/Linear-runtime-and-constant-space-solution
2+
//这道题目还没有完全理解
3+
bool isMatch(char* s, char* p) {
4+
char *ps=s;
5+
char *pp=p;
6+
char *fs=ps;
7+
char *fp=NULL;
8+
while(*ps){ //注意是*ps,不是ps
9+
printf("%c %c\n",*ps,*pp);
10+
if((*pp=='?') || (*ps==*pp)){
11+
pp++;
12+
ps++;
13+
continue;
14+
}
15+
16+
if(*pp=='*'){
17+
fp=pp++;
18+
fs=ps;
19+
continue;
20+
}
21+
22+
if(fp){
23+
pp=fp+1;
24+
ps=++fs;
25+
continue;
26+
}
27+
28+
return false;
29+
30+
}
31+
while(*pp=='*') pp++;
32+
33+
return !(*pp);
34+
}

0045.jump-game-ii/jump-game-ii.cpp

+29-52
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,29 @@
1-
/*
2-
* @lc app=leetcode id=45 lang=cpp
3-
*
4-
* [45] Jump Game II
5-
*
6-
* https://leetcode.com/problems/jump-game-ii/description/
7-
*
8-
* algorithms
9-
* Hard (27.66%)
10-
* Total Accepted: 159.5K
11-
* Total Submissions: 576.1K
12-
* Testcase Example: '[2,3,1,1,4]'
13-
*
14-
* Given an array of non-negative integers, you are initially positioned at the
15-
* first index of the array.
16-
*
17-
* Each element in the array represents your maximum jump length at that
18-
* position.
19-
*
20-
* Your goal is to reach the last index in the minimum number of jumps.
21-
*
22-
* Example:
23-
*
24-
*
25-
* Input: [2,3,1,1,4]
26-
* Output: 2
27-
* Explanation: The minimum number of jumps to reach the last index is 2.
28-
* ⁠ Jump 1 step from index 0 to 1, then 3 steps to the last index.
29-
*
30-
* Note:
31-
*
32-
* You can assume that you can always reach the last index.
33-
*
34-
*/
35-
class Solution {
36-
public:
37-
// https://www.cnblogs.com/lichen782/p/leetcode_Jump_Game_II.html
38-
int jump(vector<int>& nums) {
39-
int last=0;
40-
int curr=0;
41-
int step=0;
42-
for(int i=0;i<nums.size();i++){
43-
if(i>last){
44-
last=curr;
45-
step++;
46-
}
47-
curr=max(curr,i+nums[i]);
48-
}
49-
return step;
50-
}
51-
};
52-
1+
class Solution {
2+
public:
3+
// https://www.cnblogs.com/lichen782/p/leetcode_Jump_Game_II.html
4+
int jump1(vector<int>& nums) {
5+
int last=0;
6+
int curr=0;
7+
int step=0;
8+
for(int i=0;i<nums.size();i++){
9+
if(i>last){
10+
last=curr;
11+
step++;
12+
}
13+
curr=max(curr,i+nums[i]);
14+
}
15+
return step;
16+
}
17+
18+
// https://www.acwing.com/solution/LeetCode/content/107/
19+
int jump(vector<int>& nums) {
20+
vector<int> dp(nums.size());
21+
int last=0;
22+
for(int i=1;i<dp.size();i++){
23+
while(i>last+nums[last]) last++;
24+
dp[i]=dp[last]+1;
25+
}
26+
return dp.back();
27+
}
28+
};
29+

0054.spiral-matrix/spiral-matrix.cpp

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
class Solution {
22
public:
3+
// 做得不是很流畅
4+
vector<int> spiralOrder(vector<vector<int>>& matrix) {
5+
vector<int> res;
6+
row=matrix.size();
7+
if(row==0) return res;
8+
col=matrix[0].size();
9+
vector<vector<bool>> vis(row,vector<bool>(col,false));
10+
11+
int k=0;
12+
int x=0,y=0;
13+
14+
while(res.size()!=row*col){
15+
res.push_back(matrix[x][y]);
16+
if(res.size()==row*col) break;
17+
vis[x][y]=true;
18+
while(x+dx[k]<0 || x+dx[k]>=row || y+dy[k]<0 || y+dy[k]>=col || vis[x+dx[k]][y+dy[k]]){
19+
k++;
20+
k%=4;
21+
}
22+
x=x+dx[k];
23+
y=y+dy[k];
24+
25+
}
26+
return res;
27+
}
28+
329

430
// 三刷,自己做出来了
531
// 更简洁的做法,没有while,只有if:https://www.acwing.com/solution/acwing/content/748/
@@ -9,7 +35,7 @@ class Solution {
935
vector<int> dy={1,0,-1,0};
1036
vector<int> res;
1137
int k=0;
12-
vector<int> spiralOrder(vector<vector<int> > matrix) {
38+
vector<int> spiralOrder3(vector<vector<int> > matrix) {
1339

1440
row=matrix.size();
1541
if(row==0) return res;

0055.jump-game/jump-game.cpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
class Solution {
22
public:
3-
// 三刷,做出来了
4-
// 注意:如果当前节点可以到达,则它之前所有的节点都是可以的到达的。
53
bool canJump(vector<int>& nums) {
6-
int maxd=0;
4+
int mv=0;
75
for(int i=0;i<nums.size();i++){
8-
if(maxd<i) return false;
9-
maxd=max(maxd,i+nums[i]);
6+
if(mv<i) return false;
7+
mv=max(mv,i+nums[i]);
108
}
119
return true;
12-
1310
}
1411
};

0 commit comments

Comments
 (0)