mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 07:06:42 +08:00
Update
This commit is contained in:
@ -7,6 +7,7 @@ LeetCode 最强题解(持续更新中):
|
||||
|[0000.两数之和](https://github.com/youngyangyang04/leetcode/blob/master/problems/0000.两数之和.md) | 数组|简单|**暴力** **哈希**|
|
||||
|[0015.三数之和](https://github.com/youngyangyang04/leetcode/blob/master/problems/0015.三数之和.md) | 数组 |中等|**双指针** **哈希**|
|
||||
|[0018.四数之和](https://github.com/youngyangyang04/leetcode/blob/master/problems/0018.四数之和) | 数组 |中等|**双指针**|
|
||||
|[0020.有效的括号](https://github.com/youngyangyang04/leetcode/blob/master/problems/0020.有效的括号) | 栈 |简单|**栈**|
|
||||
|[0021.合并两个有序链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0021.合并两个有序链表.md) |链表 |简单|**模拟** |
|
||||
|[0026.删除排序数组中的重复项](https://github.com/youngyangyang04/leetcode/blob/master/problems/0026.删除排序数组中的重复项.md) |数组 |简单|**暴力** **快慢指针** |
|
||||
|[0027.移除元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0027.移除元素.md) |数组 |简单| **暴力** **快慢指针/双指针**|
|
||||
@ -17,12 +18,15 @@ LeetCode 最强题解(持续更新中):
|
||||
|[0083.删除排序链表中的重复元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0083.删除排序链表中的重复元素.md) |链表 |简单|**模拟**|
|
||||
|[0142.环形链表II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0142.环形链表II.md) |链表 |中等|**快慢指针/双指针**|
|
||||
|[0151.翻转字符串里的单词](https://github.com/youngyangyang04/leetcode/blob/master/problems/0151.翻转字符串里的单词.md) |字符串 |中等|**模拟/双指针**|
|
||||
|[0155.最小栈](https://github.com/youngyangyang04/leetcode/blob/master/problems/0155.最小栈.md) |栈 |简单|**栈**|
|
||||
|[0202.快乐数](https://github.com/youngyangyang04/leetcode/blob/master/problems/0202.快乐数.md) |哈希表 |简单|**哈希**|
|
||||
|[0203.移除链表元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0203.移除链表元素.md) |链表 |简单|**模拟** **虚拟头结点**|
|
||||
|[0205.同构字符串](https://github.com/youngyangyang04/leetcode/blob/master/problems/0205.同构字符串.md) |哈希表 |简单| **哈希**|
|
||||
|[0206.翻转链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0206.翻转链表.md) |链表 |简单| **模拟** **递归**|
|
||||
|[0209.长度最小的子数组](https://github.com/youngyangyang04/leetcode/blob/master/problems/0209.长度最小的子数组.md) |数组 |中等| **暴力** **滑动窗口**|
|
||||
|[0219.存在重复元素II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0219.存在重复元素II.md) | 哈希表 |简单| **哈希** |
|
||||
|[0225.用队列实现栈](https://github.com/youngyangyang04/leetcode/blob/master/problems/0225.用队列实现栈.md) | 队列 |简单| **队列** |
|
||||
|[0232.用栈实现队列](https://github.com/youngyangyang04/leetcode/blob/master/problems/0232.用栈实现队列.md) | 栈 |简单| **栈** |
|
||||
|[0237.删除链表中的节点](https://github.com/youngyangyang04/leetcode/blob/master/problems/0237.删除链表中的节点.md) |链表 |简单| **原链表移除** **添加虚拟节点** 递归|
|
||||
|[0242.有效的字母异位词](https://github.com/youngyangyang04/leetcode/blob/master/problems/0242.有效的字母异位词.md) |哈希表 |简单| **哈希**|
|
||||
|[0344.反转字符串](https://github.com/youngyangyang04/leetcode/blob/master/problems/0344.反转字符串.md) |字符串 |简单| **双指针**|
|
||||
@ -34,6 +38,7 @@ LeetCode 最强题解(持续更新中):
|
||||
|[0575.分糖果.md](https://github.com/youngyangyang04/leetcode/blob/master/problems/0575.分糖果.md) |哈希表 |简单|**哈希**|
|
||||
|[0705.设计哈希集合](https://github.com/youngyangyang04/leetcode/blob/master/problems/0705.设计哈希集合.md) |哈希表 |简单|**模拟**|
|
||||
|[0707.设计链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0707.设计链表.md) |链表 |中等|**模拟**|
|
||||
|[1047.删除字符串中的所有相邻重复项](https://github.com/youngyangyang04/leetcode/blob/master/problems/1047.删除字符串中的所有相邻重复项.md) |栈 |简单|**栈**|
|
||||
|[剑指Offer05.替换空格](https://github.com/youngyangyang04/leetcode/blob/master/problems/剑指Offer05.替换空格.md) |字符串 |简单|**双指针**|
|
||||
|
||||
Leetcode精选:
|
||||
|
26
problems/0020.有效的括号.md
Normal file
26
problems/0020.有效的括号.md
Normal file
@ -0,0 +1,26 @@
|
||||
## 题目地址
|
||||
https://leetcode-cn.com/problems/valid-parentheses/
|
||||
|
||||
## 思路
|
||||
|
||||
括号匹配是使用栈解决的经典问题
|
||||
|
||||
## C++代码
|
||||
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
bool isValid(string s) {
|
||||
stack<int> st;
|
||||
for (int i = 0; i < s.size(); i++) {
|
||||
if (s[i] == '(') st.push(')');
|
||||
else if (s[i] == '{') st.push('}');
|
||||
else if (s[i] == '[') st.push(']');
|
||||
else if (st.empty() || st.top() != s[i]) return false;
|
||||
else st.pop();
|
||||
}
|
||||
return st.empty();
|
||||
}
|
||||
};
|
||||
```
|
||||
> 更多精彩文章持续更新,可以微信搜索「 代码随想录」第一时间阅读,关注后有大量的学习资料和简历模板可以免费领取,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
50
problems/0155.最小栈.md
Normal file
50
problems/0155.最小栈.md
Normal file
@ -0,0 +1,50 @@
|
||||
## 题目地址
|
||||
https://leetcode-cn.com/problems/min-stack/
|
||||
|
||||
## 思路
|
||||
|
||||
有的同学一开始会把这道题目想简单了,用一个变量记录最小值不就得了,其实是如果要是弹出了这个最小值的话,我们还要记录次小值,所以需要一个辅助数组来记录次小值。
|
||||
|
||||
我这里使用数组来实现栈,在用一个数组来放当前栈里最小数值,同时使用辅助数组来记录
|
||||
|
||||
## C++代码
|
||||
|
||||
```
|
||||
class MinStack {
|
||||
public:
|
||||
vector<int> vec;
|
||||
vector<int> minVec;
|
||||
/** initialize your data structure here. */
|
||||
MinStack() {
|
||||
}
|
||||
void push(int x) {
|
||||
vec.push_back(x);
|
||||
if (minVec.size() == 0) {
|
||||
minVec.push_back(x);
|
||||
} else if (x <= minVec[minVec.size() - 1]) { // 这里一定是下小于等于,防止多个最小值的情况
|
||||
minVec.push_back(x);
|
||||
}
|
||||
}
|
||||
void pop() {
|
||||
if (vec.size() == 0) { // 防止下面的操作会导致越界
|
||||
return;
|
||||
}
|
||||
if (vec[vec.size() - 1] == minVec[minVec.size() - 1]) {
|
||||
minVec.pop_back();
|
||||
}
|
||||
vec.pop_back();
|
||||
}
|
||||
|
||||
int top() {
|
||||
// 这里有越界的危险,但是题目也没有说如果栈为空,top()应该返回啥,所以就默认测试用例没有上来直接top的用例了
|
||||
return vec[vec.size() - 1];
|
||||
}
|
||||
|
||||
int getMin() {
|
||||
// 这里有越界的危险,但是题目也没有说如果栈为空,getMin()应该返回啥,所以就默认测试用例没有上来直接getMin的用例了
|
||||
return minVec[minVec.size() - 1];
|
||||
}
|
||||
};
|
||||
|
||||
```
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
@ -1,4 +1,9 @@
|
||||
## 题目地址
|
||||
|
||||
## 思路
|
||||
|
||||
|
||||
## C++代码
|
||||
|
||||
|
||||
```
|
||||
|
59
problems/0225.用队列实现栈.md
Normal file
59
problems/0225.用队列实现栈.md
Normal file
@ -0,0 +1,59 @@
|
||||
## 题目地址
|
||||
|
||||
https://leetcode-cn.com/problems/implement-stack-using-queues/
|
||||
|
||||
## 思路
|
||||
|
||||
用栈实现队列, 和用队列实现栈的思路还是不一样的,这取决于这两个数据结构的性质
|
||||
|
||||
建议大家题目编号: 225 和 232 一起做来做
|
||||
|
||||
详细如代码注释所示
|
||||
|
||||
## C++代码
|
||||
|
||||
```
|
||||
class MyStack {
|
||||
public:
|
||||
queue<int> queIn;
|
||||
queue<int> queOut;
|
||||
/** Initialize your data structure here. */
|
||||
MyStack() {
|
||||
|
||||
}
|
||||
|
||||
/** Push element x onto stack. */
|
||||
void push(int x) {
|
||||
queIn.push(x);
|
||||
}
|
||||
|
||||
/** Removes the element on top of the stack and returns that element. */
|
||||
int pop() {
|
||||
int size = queIn.size();
|
||||
size--;
|
||||
while (size--) { // 将queIn 导入queOut,但要留下最后一个元素
|
||||
queOut.push(queIn.front());
|
||||
queIn.pop();
|
||||
}
|
||||
|
||||
int result = queIn.front(); // 留下的最后一个元素就是我们要返回的值
|
||||
queIn.pop();
|
||||
queIn = queOut; // 再将queOut赋值给queIn
|
||||
while(!queOut.empty()) { // 清空queOut
|
||||
queOut.pop();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Get the top element. */
|
||||
int top() {
|
||||
return queIn.back();
|
||||
}
|
||||
|
||||
/** Returns whether the stack is empty. */
|
||||
bool empty() {
|
||||
return queIn.empty();
|
||||
}
|
||||
};
|
||||
```
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
59
problems/0232.用栈实现队列.md
Normal file
59
problems/0232.用栈实现队列.md
Normal file
@ -0,0 +1,59 @@
|
||||
## 题目地址
|
||||
https://leetcode-cn.com/problems/implement-queue-using-stacks/
|
||||
|
||||
## 思路
|
||||
|
||||
使用两个栈 一个输入栈,一个输出栈, 这里要注意输入栈和输出栈的关系,每当pop的时候且输出栈为空,就应该把输入站全部导入输出栈中
|
||||
|
||||
## C++代码
|
||||
|
||||
```
|
||||
class MyQueue {
|
||||
public:
|
||||
stack<int> stIn;
|
||||
stack<int> stOut;
|
||||
/** Initialize your data structure here. */
|
||||
MyQueue() {
|
||||
|
||||
}
|
||||
/** Push element x to the back of queue. */
|
||||
void push(int x) {
|
||||
stIn.push(x);
|
||||
}
|
||||
|
||||
/** Removes the element from in front of queue and returns that element. */
|
||||
int pop() {
|
||||
if (stOut.empty()) { //只有当stOut为空的时候,再从stIn里导入数据
|
||||
while(!stIn.empty()) {
|
||||
stOut.push(stIn.top()); // 从stIn导入数据知道stOut为空
|
||||
stIn.pop();
|
||||
}
|
||||
}
|
||||
int result = stOut.top();
|
||||
stOut.pop();
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Get the front element. */
|
||||
int peek() {
|
||||
int res = this->pop(); // 直接使用已有的pop函数
|
||||
stOut.push(res); // 因为pop函数弹出了元素res,所以再添加回去
|
||||
return res;
|
||||
}
|
||||
|
||||
/** Returns whether the queue is empty. */
|
||||
bool empty() {
|
||||
return stIn.empty() && stOut.empty();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Your MyQueue object will be instantiated and called as such:
|
||||
* MyQueue* obj = new MyQueue();
|
||||
* obj->push(x);
|
||||
* int param_2 = obj->pop();
|
||||
* int param_3 = obj->peek();
|
||||
* bool param_4 = obj->empty();
|
||||
*/
|
||||
```
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
32
problems/1047.删除字符串中的所有相邻重复项.md
Normal file
32
problems/1047.删除字符串中的所有相邻重复项.md
Normal file
@ -0,0 +1,32 @@
|
||||
## 题目地址
|
||||
https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/
|
||||
|
||||
## 思路
|
||||
|
||||
这道题目就像是我们玩过的游戏对对碰, 可以把字符串放到与一个栈中,然后如果相同的话 栈就弹出,这样最后栈里剩下的元素都是相邻不相同的元素了
|
||||
|
||||
## C++代码
|
||||
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
string removeDuplicates(string S) {
|
||||
stack<char> st;
|
||||
for (char s : S) {
|
||||
if (st.empty() || s != st.top()) {
|
||||
st.push(s);
|
||||
} else {
|
||||
st.pop();
|
||||
}
|
||||
}
|
||||
string result = "";
|
||||
while(!st.empty()) {
|
||||
result += st.top();
|
||||
st.pop();
|
||||
}
|
||||
reverse(result.begin(), result.end()); // 此时字符串需要反转一下
|
||||
return result;
|
||||
|
||||
}
|
||||
};
|
||||
```
|
Reference in New Issue
Block a user