mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
Update
This commit is contained in:
40
README.md
40
README.md
@ -1,6 +1,23 @@
|
|||||||
# leetcode
|
# Leetcode精选:
|
||||||
|
|
||||||
LeetCode 最强题解(持续更新中):
|
* [leetcode面试高频题目题解](https://github.com/youngyangyang04/leetcode-master)
|
||||||
|
* [究竟什么是时间复杂度,怎么求时间复杂度,看这一篇就够了](https://mp.weixin.qq.com/s/ma615my-adxMNKmzcdYVlg)
|
||||||
|
* [一文带你彻底理解程序为什么会超时](https://mp.weixin.qq.com/s/ZLHsvkxppD8QJMBj3njOiw)
|
||||||
|
* [一场面试,带你彻底掌握递归算法的时间复杂度](https://mp.weixin.qq.com/s/GOYVzUVrfLUjOrDg21YOAw)
|
||||||
|
* [算法分析中的空间复杂度,你真的会了么?](https://mp.weixin.qq.com/s/uU6cn0SYRUbIf5tU2QkcOQ)
|
||||||
|
* [二分法其实很简单,为什么老是写不对!!](https://mp.weixin.qq.com/s/mZ87E2vdvqhbv55uYpdrAQ)
|
||||||
|
* [程序员算法面试中,必须掌握的数组理论知识](https://mp.weixin.qq.com/s/hgSxlgs12IQPUvG0BQukBg)
|
||||||
|
* [这五道数组相关的面试题,你一定要会!](https://mp.weixin.qq.com/s/tMgABUtR1Ydv330NogS3Jg)
|
||||||
|
* [关于哈希表,你该了解这些!](https://mp.weixin.qq.com/s/UnuQIRZSn_i2G4VhTWK5CQ)
|
||||||
|
* 精选链表相关的面试题
|
||||||
|
* 精选哈希表相关的面试题
|
||||||
|
* 精选字符串相关的面试题
|
||||||
|
* 精选栈与队列相关的面试题
|
||||||
|
* 精选二叉树相关的面试题
|
||||||
|
* 精选递归与回溯面试题
|
||||||
|
|
||||||
|
|
||||||
|
# LeetCode 最强题解:
|
||||||
|
|
||||||
|题目 | 类型 | 难度 | 解题方法 |
|
|题目 | 类型 | 难度 | 解题方法 |
|
||||||
|---|---| ---| --- |
|
|---|---| ---| --- |
|
||||||
@ -28,8 +45,10 @@ LeetCode 最强题解(持续更新中):
|
|||||||
|[0225.用队列实现栈](https://github.com/youngyangyang04/leetcode/blob/master/problems/0225.用队列实现栈.md) | 队列 |简单| **队列** |
|
|[0225.用队列实现栈](https://github.com/youngyangyang04/leetcode/blob/master/problems/0225.用队列实现栈.md) | 队列 |简单| **队列** |
|
||||||
|[0232.用栈实现队列](https://github.com/youngyangyang04/leetcode/blob/master/problems/0232.用栈实现队列.md) | 栈 |简单| **栈** |
|
|[0232.用栈实现队列](https://github.com/youngyangyang04/leetcode/blob/master/problems/0232.用栈实现队列.md) | 栈 |简单| **栈** |
|
||||||
|[0237.删除链表中的节点](https://github.com/youngyangyang04/leetcode/blob/master/problems/0237.删除链表中的节点.md) |链表 |简单| **原链表移除** **添加虚拟节点** 递归|
|
|[0237.删除链表中的节点](https://github.com/youngyangyang04/leetcode/blob/master/problems/0237.删除链表中的节点.md) |链表 |简单| **原链表移除** **添加虚拟节点** 递归|
|
||||||
|
|[0239.滑动窗口最大值](https://github.com/youngyangyang04/leetcode/blob/master/problems/0239.滑动窗口最大值.md) |滑动窗口/队列 |困难| **单调队列**|
|
||||||
|[0242.有效的字母异位词](https://github.com/youngyangyang04/leetcode/blob/master/problems/0242.有效的字母异位词.md) |哈希表 |简单| **哈希**|
|
|[0242.有效的字母异位词](https://github.com/youngyangyang04/leetcode/blob/master/problems/0242.有效的字母异位词.md) |哈希表 |简单| **哈希**|
|
||||||
|[0344.反转字符串](https://github.com/youngyangyang04/leetcode/blob/master/problems/0344.反转字符串.md) |字符串 |简单| **双指针**|
|
|[0344.反转字符串](https://github.com/youngyangyang04/leetcode/blob/master/problems/0344.反转字符串.md) |字符串 |简单| **双指针**|
|
||||||
|
|[0347.前K个高频元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0347.前K个高频元素.md) |哈希/堆/优先级队列 |中等| **哈希/优先级队列**|
|
||||||
|[0349.两个数组的交集](https://github.com/youngyangyang04/leetcode/blob/master/problems/0349.两个数组的交集.md) |哈希表 |简单|**哈希**|
|
|[0349.两个数组的交集](https://github.com/youngyangyang04/leetcode/blob/master/problems/0349.两个数组的交集.md) |哈希表 |简单|**哈希**|
|
||||||
|[0350.两个数组的交集II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0350.两个数组的交集II.md) |哈希表 |简单|**哈希**|
|
|[0350.两个数组的交集II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0350.两个数组的交集II.md) |哈希表 |简单|**哈希**|
|
||||||
|[0383.赎金信](https://github.com/youngyangyang04/leetcode/blob/master/problems/0383.赎金信.md) |数组 |简单|**暴力** **字典计数** **哈希**|
|
|[0383.赎金信](https://github.com/youngyangyang04/leetcode/blob/master/problems/0383.赎金信.md) |数组 |简单|**暴力** **字典计数** **哈希**|
|
||||||
@ -41,15 +60,18 @@ LeetCode 最强题解(持续更新中):
|
|||||||
|[1047.删除字符串中的所有相邻重复项](https://github.com/youngyangyang04/leetcode/blob/master/problems/1047.删除字符串中的所有相邻重复项.md) |栈 |简单|**栈**|
|
|[1047.删除字符串中的所有相邻重复项](https://github.com/youngyangyang04/leetcode/blob/master/problems/1047.删除字符串中的所有相邻重复项.md) |栈 |简单|**栈**|
|
||||||
|[剑指Offer05.替换空格](https://github.com/youngyangyang04/leetcode/blob/master/problems/剑指Offer05.替换空格.md) |字符串 |简单|**双指针**|
|
|[剑指Offer05.替换空格](https://github.com/youngyangyang04/leetcode/blob/master/problems/剑指Offer05.替换空格.md) |字符串 |简单|**双指针**|
|
||||||
|
|
||||||
Leetcode精选:
|
|
||||||
|
|
||||||
* [精选数组相关的面试题](https://mp.weixin.qq.com/s?__biz=Mzg5MTExMTk2OA==&mid=2247484000&idx=1&sn=48e87c8ca22390ac5e78bcbb45e2052c&chksm=cfd31d2ff8a4943916305f519d963d1c0979f112abd6f613af7bdb7fa6ec3f86e8c93c2e0caa&token=411961669&lang=zh_CN#rd)
|
|
||||||
|
|
||||||
* [精选链表相关的面试题]()
|
|
||||||
|
|
||||||
持续更新中....
|
持续更新中....
|
||||||
|
|
||||||
> 笔者介绍:
|
> 笔者介绍:
|
||||||
> ACM亚洲区域赛铜牌获得者,哈工大计算机硕士毕业后,先后在腾讯和百度工作多年,对算法和后端技术有一定的见解,利用工作之余重新刷leetcode
|
> ACM亚洲区域赛铜牌获得者,哈工大计算机硕士毕业后,先后在腾讯和百度工作多年,对算法和后端技术有一定的见解,利用工作之余重新刷leetcode
|
||||||
> 欢迎关注微信公众号:「代码随想录」,这里将持续分享自己对互联网以及技术的想法与思考
|
> 更多精彩文章持续更新,微信搜索:「代码随想录」第一时间围观,关注后回复: 「简历模板」「java」「C++」「python」「算法与数据结构」 等关键字就可以获得我多年整理出来的学习资料。
|
||||||
|
|
||||||
|
# 联系我
|
||||||
|
|
||||||
|
<a name="微信"></a>
|
||||||
|
<img src="https://img-blog.csdnimg.cn/20200712232919673.jpeg" data-img="1" width="175" height="175">
|
||||||
|
|
||||||
|
# 我的公众号
|
||||||
|
<a name="公众号"></a>
|
||||||
|
<img src="https://img-blog.csdnimg.cn/20200323202104335.jpg" data-img="1" width="200" height="200">
|
||||||
|
|
||||||
|
33
problems/0150.逆波兰表达式求值.md
Normal file
33
problems/0150.逆波兰表达式求值.md
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
## 题目地址
|
||||||
|
https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/
|
||||||
|
|
||||||
|
## 思路
|
||||||
|
|
||||||
|
这道题目相当于是二叉树中的后序遍历,也引申出栈与递归之间在某种程度上是可以转换的!
|
||||||
|
|
||||||
|
## C++代码
|
||||||
|
|
||||||
|
```
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int evalRPN(vector<string>& tokens) {
|
||||||
|
stack<int> st;
|
||||||
|
for (int i = 0; i < tokens.size(); i++) {
|
||||||
|
if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") {
|
||||||
|
int num1 = st.top();
|
||||||
|
st.pop();
|
||||||
|
int num2 = st.top();
|
||||||
|
st.pop();
|
||||||
|
if (tokens[i] == "+") st.push(num2 + num1);
|
||||||
|
if (tokens[i] == "-") st.push(num2 - num1);
|
||||||
|
if (tokens[i] == "*") st.push(num2 * num1);
|
||||||
|
if (tokens[i] == "/") st.push(num2 / num1);
|
||||||
|
} else {
|
||||||
|
st.push(stoi(tokens[i]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return st.top();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
||||||
|
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
46
problems/0239.滑动窗口最大值.md
Normal file
46
problems/0239.滑动窗口最大值.md
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
## 题目地址
|
||||||
|
https://leetcode-cn.com/problems/sliding-window-maximum/
|
||||||
|
|
||||||
|
## 思路
|
||||||
|
|
||||||
|
|
||||||
|
## C++代码
|
||||||
|
|
||||||
|
```
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
class MyQueue {
|
||||||
|
public:
|
||||||
|
deque<int> que;
|
||||||
|
void pop(int value) {
|
||||||
|
if (!que.empty() && value == que.front()) {
|
||||||
|
que.pop_front();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void push(int value) {
|
||||||
|
while (!que.empty() && value > que.back()) {
|
||||||
|
que.pop_back();
|
||||||
|
}
|
||||||
|
que.push_back(value);
|
||||||
|
|
||||||
|
}
|
||||||
|
int front() {
|
||||||
|
return que.front();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
|
||||||
|
MyQueue que;
|
||||||
|
vector<int> result;
|
||||||
|
for (int i = 0; i < k; i++) {
|
||||||
|
que.push(nums[i]);
|
||||||
|
}
|
||||||
|
result.push_back(que.front());
|
||||||
|
for (int i = k; i < nums.size(); i++) {
|
||||||
|
que.pop(nums[i - k]);
|
||||||
|
que.push(nums[i]);
|
||||||
|
result.push_back(que.front());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
41
problems/0347.前K个高频元素.md
Normal file
41
problems/0347.前K个高频元素.md
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
## 题目地址
|
||||||
|
|
||||||
|
## 思路
|
||||||
|
|
||||||
|
|
||||||
|
## C++代码
|
||||||
|
```
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
class mycomparison {
|
||||||
|
public:
|
||||||
|
bool operator()(const pair<int, int>& lhs, const pair<int, int>& rhs) {
|
||||||
|
return lhs.second < rhs.second;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// 如何定义priQue
|
||||||
|
// 如何定义cmp
|
||||||
|
// 如何遍历map
|
||||||
|
vector<int> topKFrequent(vector<int>& nums, int k) {
|
||||||
|
unordered_map<int, int> map;
|
||||||
|
for (int i = 0; i < nums.size(); i++) {
|
||||||
|
map[nums[i]]++;
|
||||||
|
}
|
||||||
|
// greater<pair<int, int>> 也可以
|
||||||
|
priority_queue<pair<int, int>, vector<pair<int, int>>, mycomparison> pri_que;
|
||||||
|
|
||||||
|
// 当然可以使用auto
|
||||||
|
for (unordered_map<int, int>::iterator it = map.begin(); it != map.end(); it++) {
|
||||||
|
pri_que.push(*it);
|
||||||
|
}
|
||||||
|
// 一开始就确定空间
|
||||||
|
vector<int> result(k);
|
||||||
|
for (int i = 0; i < k; i++) {
|
||||||
|
result[i] = pri_que.top().first;
|
||||||
|
pri_que.pop();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
@ -3,8 +3,9 @@ https://leetcode-cn.com/problems/distribute-candies/
|
|||||||
|
|
||||||
## 思路
|
## 思路
|
||||||
|
|
||||||
因为种类是可妹妹先来,所以思路先求出一共有多少种糖果,然后如果糖果种类大于candies的一半了,return candies的一半,否则,就是return 糖果的种类就可以了。
|
糖果的种类是可妹妹先来,所以思路先求出一共有多少种类型的糖果,需要注意: 数组中数字的大小也就是糖果的种类取值范围在[负十万和 正十万之间], 依然可以定义一个数组,通过哈希法求出有多少类型的糖果,那么糖果种类可以是负数 怎么办呢,可以把 定一个 20万大小的数组 ,就可以把糖果的全部类型映射到数组的下表了。
|
||||||
|
|
||||||
|
通过哈希法,可以求出了糖果的类型数量,如果糖果种类大于糖果总数的一半了,返回 糖果数量的一半就好,因为妹妹已经得到种类最多的糖果了,否则,就是返回 糖果的种类。
|
||||||
|
|
||||||
## 代码
|
## 代码
|
||||||
|
|
||||||
@ -34,4 +35,6 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
|
||||||
|
> 更过算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user