Skip to content

Commit 0e1f22b

Browse files
Update
1 parent e32c005 commit 0e1f22b

File tree

7 files changed

+236
-0
lines changed

7 files changed

+236
-0
lines changed

‎README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ LeetCode 最强题解(持续更新中):
77
|[0000.两数之和](https://github.com/youngyangyang04/leetcode/blob/master/problems/0000.两数之和.md)| 数组|简单|**暴力****哈希**|
88
|[0015.三数之和](https://github.com/youngyangyang04/leetcode/blob/master/problems/0015.三数之和.md)| 数组 |中等|**双指针****哈希**|
99
|[0018.四数之和](https://github.com/youngyangyang04/leetcode/blob/master/problems/0018.四数之和)| 数组 |中等|**双指针**|
10+
|[0020.有效的括号](https://github.com/youngyangyang04/leetcode/blob/master/problems/0020.有效的括号)||简单|****|
1011
|[0021.合并两个有序链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0021.合并两个有序链表.md)|链表 |简单|**模拟**|
1112
|[0026.删除排序数组中的重复项](https://github.com/youngyangyang04/leetcode/blob/master/problems/0026.删除排序数组中的重复项.md)|数组 |简单|**暴力****快慢指针**|
1213
|[0027.移除元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0027.移除元素.md)|数组 |简单|**暴力****快慢指针/双指针**|
@@ -17,12 +18,15 @@ LeetCode 最强题解(持续更新中):
1718
|[0083.删除排序链表中的重复元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0083.删除排序链表中的重复元素.md)|链表 |简单|**模拟**|
1819
|[0142.环形链表II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0142.环形链表II.md)|链表 |中等|**快慢指针/双指针**|
1920
|[0151.翻转字符串里的单词](https://github.com/youngyangyang04/leetcode/blob/master/problems/0151.翻转字符串里的单词.md)|字符串 |中等|**模拟/双指针**|
21+
|[0155.最小栈](https://github.com/youngyangyang04/leetcode/blob/master/problems/0155.最小栈.md)||简单|****|
2022
|[0202.快乐数](https://github.com/youngyangyang04/leetcode/blob/master/problems/0202.快乐数.md)|哈希表 |简单|**哈希**|
2123
|[0203.移除链表元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0203.移除链表元素.md)|链表 |简单|**模拟****虚拟头结点**|
2224
|[0205.同构字符串](https://github.com/youngyangyang04/leetcode/blob/master/problems/0205.同构字符串.md)|哈希表 |简单|**哈希**|
2325
|[0206.翻转链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0206.翻转链表.md)|链表 |简单|**模拟****递归**|
2426
|[0209.长度最小的子数组](https://github.com/youngyangyang04/leetcode/blob/master/problems/0209.长度最小的子数组.md)|数组 |中等|**暴力****滑动窗口**|
2527
|[0219.存在重复元素II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0219.存在重复元素II.md)| 哈希表 |简单|**哈希**|
28+
|[0225.用队列实现栈](https://github.com/youngyangyang04/leetcode/blob/master/problems/0225.用队列实现栈.md)| 队列 |简单|**队列**|
29+
|[0232.用栈实现队列](https://github.com/youngyangyang04/leetcode/blob/master/problems/0232.用栈实现队列.md)||简单|****|
2630
|[0237.删除链表中的节点](https://github.com/youngyangyang04/leetcode/blob/master/problems/0237.删除链表中的节点.md)|链表 |简单|**原链表移除****添加虚拟节点** 递归|
2731
|[0242.有效的字母异位词](https://github.com/youngyangyang04/leetcode/blob/master/problems/0242.有效的字母异位词.md)|哈希表 |简单|**哈希**|
2832
|[0344.反转字符串](https://github.com/youngyangyang04/leetcode/blob/master/problems/0344.反转字符串.md)|字符串 |简单|**双指针**|
@@ -34,6 +38,7 @@ LeetCode 最强题解(持续更新中):
3438
|[0575.分糖果.md](https://github.com/youngyangyang04/leetcode/blob/master/problems/0575.分糖果.md)|哈希表 |简单|**哈希**|
3539
|[0705.设计哈希集合](https://github.com/youngyangyang04/leetcode/blob/master/problems/0705.设计哈希集合.md)|哈希表 |简单|**模拟**|
3640
|[0707.设计链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0707.设计链表.md)|链表 |中等|**模拟**|
41+
|[1047.删除字符串中的所有相邻重复项](https://github.com/youngyangyang04/leetcode/blob/master/problems/1047.删除字符串中的所有相邻重复项.md)||简单|****|
3742
|[剑指Offer05.替换空格](https://github.com/youngyangyang04/leetcode/blob/master/problems/剑指Offer05.替换空格.md)|字符串 |简单|**双指针**|
3843

3944
Leetcode精选:

‎problems/0020.有效的括号.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## 题目地址
2+
https://leetcode-cn.com/problems/valid-parentheses/
3+
4+
## 思路
5+
6+
括号匹配是使用栈解决的经典问题
7+
8+
## C++代码
9+
10+
```
11+
class Solution{
12+
public:
13+
bool isValid(string s){
14+
stack<int> st;
15+
for (int i = 0; i < s.size(); i++){
16+
if (s[i] == '(') st.push(')');
17+
else if (s[i] == '{') st.push('}');
18+
else if (s[i] == '[') st.push(']');
19+
else if (st.empty() || st.top() != s[i]) return false;
20+
else st.pop();
21+
}
22+
return st.empty();
23+
}
24+
};
25+
```
26+
> 更多精彩文章持续更新,可以微信搜索「 代码随想录」第一时间阅读,关注后有大量的学习资料和简历模板可以免费领取,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master)https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。

‎problems/0155.最小栈.md‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## 题目地址
2+
https://leetcode-cn.com/problems/min-stack/
3+
4+
## 思路
5+
6+
有的同学一开始会把这道题目想简单了,用一个变量记录最小值不就得了,其实是如果要是弹出了这个最小值的话,我们还要记录次小值,所以需要一个辅助数组来记录次小值。
7+
8+
我这里使用数组来实现栈,在用一个数组来放当前栈里最小数值,同时使用辅助数组来记录
9+
10+
## C++代码
11+
12+
```
13+
class MinStack{
14+
public:
15+
vector<int> vec;
16+
vector<int> minVec;
17+
/** initialize your data structure here. */
18+
MinStack(){
19+
}
20+
void push(int x){
21+
vec.push_back(x);
22+
if (minVec.size() == 0){
23+
minVec.push_back(x);
24+
} else if (x <= minVec[minVec.size() - 1]){// 这里一定是下小于等于,防止多个最小值的情况
25+
minVec.push_back(x);
26+
}
27+
}
28+
void pop(){
29+
if (vec.size() == 0){// 防止下面的操作会导致越界
30+
return;
31+
}
32+
if (vec[vec.size() - 1] == minVec[minVec.size() - 1]){
33+
minVec.pop_back();
34+
}
35+
vec.pop_back();
36+
}
37+
38+
int top(){
39+
// 这里有越界的危险,但是题目也没有说如果栈为空,top()应该返回啥,所以就默认测试用例没有上来直接top的用例了
40+
return vec[vec.size() - 1];
41+
}
42+
43+
int getMin(){
44+
// 这里有越界的危险,但是题目也没有说如果栈为空,getMin()应该返回啥,所以就默认测试用例没有上来直接getMin的用例了
45+
return minVec[minVec.size() - 1];
46+
}
47+
};
48+
49+
```
50+
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master)https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。

‎problems/0220.存在重复元素III.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
## 题目地址
12

3+
## 思路
4+
5+
6+
## C++代码
27

38

49
```
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## 题目地址
2+
3+
https://leetcode-cn.com/problems/implement-stack-using-queues/
4+
5+
## 思路
6+
7+
用栈实现队列, 和用队列实现栈的思路还是不一样的,这取决于这两个数据结构的性质
8+
9+
建议大家题目编号: 225 和 232 一起做来做
10+
11+
详细如代码注释所示
12+
13+
## C++代码
14+
15+
```
16+
class MyStack{
17+
public:
18+
queue<int> queIn;
19+
queue<int> queOut;
20+
/** Initialize your data structure here. */
21+
MyStack(){
22+
23+
}
24+
25+
/** Push element x onto stack. */
26+
void push(int x){
27+
queIn.push(x);
28+
}
29+
30+
/** Removes the element on top of the stack and returns that element. */
31+
int pop(){
32+
int size = queIn.size();
33+
size--;
34+
while (size--){// 将queIn 导入queOut,但要留下最后一个元素
35+
queOut.push(queIn.front());
36+
queIn.pop();
37+
}
38+
39+
int result = queIn.front(); // 留下的最后一个元素就是我们要返回的值
40+
queIn.pop();
41+
queIn = queOut; // 再将queOut赋值给queIn
42+
while(!queOut.empty()){// 清空queOut
43+
queOut.pop();
44+
}
45+
return result;
46+
}
47+
48+
/** Get the top element. */
49+
int top(){
50+
return queIn.back();
51+
}
52+
53+
/** Returns whether the stack is empty. */
54+
bool empty(){
55+
return queIn.empty();
56+
}
57+
};
58+
```
59+
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master)https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## 题目地址
2+
https://leetcode-cn.com/problems/implement-queue-using-stacks/
3+
4+
## 思路
5+
6+
使用两个栈 一个输入栈,一个输出栈, 这里要注意输入栈和输出栈的关系,每当pop的时候且输出栈为空,就应该把输入站全部导入输出栈中
7+
8+
## C++代码
9+
10+
```
11+
class MyQueue{
12+
public:
13+
stack<int> stIn;
14+
stack<int> stOut;
15+
/** Initialize your data structure here. */
16+
MyQueue(){
17+
18+
}
19+
/** Push element x to the back of queue. */
20+
void push(int x){
21+
stIn.push(x);
22+
}
23+
24+
/** Removes the element from in front of queue and returns that element. */
25+
int pop(){
26+
if (stOut.empty()){//只有当stOut为空的时候,再从stIn里导入数据
27+
while(!stIn.empty()){
28+
stOut.push(stIn.top()); // 从stIn导入数据知道stOut为空
29+
stIn.pop();
30+
}
31+
}
32+
int result = stOut.top();
33+
stOut.pop();
34+
return result;
35+
}
36+
37+
/** Get the front element. */
38+
int peek(){
39+
int res = this->pop(); // 直接使用已有的pop函数
40+
stOut.push(res); // 因为pop函数弹出了元素res,所以再添加回去
41+
return res;
42+
}
43+
44+
/** Returns whether the queue is empty. */
45+
bool empty(){
46+
return stIn.empty() && stOut.empty();
47+
}
48+
};
49+
50+
/**
51+
* Your MyQueue object will be instantiated and called as such:
52+
* MyQueue* obj = new MyQueue();
53+
* obj->push(x);
54+
* int param_2 = obj->pop();
55+
* int param_3 = obj->peek();
56+
* bool param_4 = obj->empty();
57+
*/
58+
```
59+
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master)https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## 题目地址
2+
https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/
3+
4+
## 思路
5+
6+
这道题目就像是我们玩过的游戏对对碰, 可以把字符串放到与一个栈中,然后如果相同的话 栈就弹出,这样最后栈里剩下的元素都是相邻不相同的元素了
7+
8+
## C++代码
9+
10+
```
11+
class Solution{
12+
public:
13+
string removeDuplicates(string S){
14+
stack<char> st;
15+
for (char s : S){
16+
if (st.empty() || s != st.top()){
17+
st.push(s);
18+
} else{
19+
st.pop();
20+
}
21+
}
22+
string result = ""
23+
while(!st.empty()){
24+
result += st.top();
25+
st.pop();
26+
}
27+
reverse(result.begin(), result.end()); // 此时字符串需要反转一下
28+
return result;
29+
30+
}
31+
};
32+
```

0 commit comments

Comments
(0)