Compare commits

..

13 Commits

Author SHA1 Message Date
ba4fe4c9a0 239. 滑动窗口最大值 2025-09-13 23:15:44 +08:00
f65b2bc248 239. 滑动窗口最大值 2025-09-13 09:03:37 +08:00
0740d30bdc 239. 滑动窗口最大值 2025-09-12 23:02:11 +08:00
124b24c361 560. 和为 K 的子数组 2025-09-12 00:42:49 +08:00
b0d0b61aa1 438. 找到字符串中所有字母异位词 2025-09-10 23:06:47 +08:00
ab592a9f3d 438. 找到字符串中所有字母异位词 2025-09-10 23:05:16 +08:00
9294aed9b4 3. 无重复字符的最长子串 2025-09-09 23:16:54 +08:00
56539bc6c1 delete test.txt 2025-09-08 23:05:56 +08:00
43c0feea86 42. 接雨水 2025-09-08 23:05:13 +08:00
aad0631b91 15. 三数之和 2025-08-15 10:13:59 +08:00
1651e74bfa 15. 三数之和 2025-08-14 23:37:03 +08:00
f8f0a3632b 11. 盛最多水的容器 2025-08-14 19:44:02 +08:00
61531cb43f 283. 移动零 2025-08-11 23:14:22 +08:00
8 changed files with 200 additions and 0 deletions

11
test004.cpp Normal file
View File

@@ -0,0 +1,11 @@
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int len = nums.size();
int index = 0;
for(int i = 0; i < len; i++){
if(nums[i]) nums[index++] = nums[i];
}
while(index < len) nums[index++] = 0;
}
};

16
test005.cpp Normal file
View File

@@ -0,0 +1,16 @@
class Solution {
public:
int maxArea(vector<int>& height) {
int len = height.size();
int l = 0, r = len - 1;
int res = 0;
while(l < r){
int area = min(height[l],height[r]) * (r - l);
res = max(area, res);
if(height[l] > height[r]) r--;
else l++;
}
return res;
}
};

28
test006.cpp Normal file
View File

@@ -0,0 +1,28 @@
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
// The main issue is to remove duplicate answers
int n = nums.size();
sort(nums.begin(), nums.end());
vector<vector<int>> ret;
int preid = -1;
for(int i = 0; i < n; i++){
if(preid != -1 && nums[i] == nums[preid]) continue;
int l = i + 1, r = n - 1;
while(l < r){
int sum = nums[i] + nums[l] + nums[r];
if(sum == 0){
ret.push_back({nums[l], nums[r], nums[i]});
int tmp = nums[l];
while(l < r && nums[l] == tmp) l++;
preid = i;
}else if(sum > 0){
r--;
}else{
l++;
}
}
}
return ret;
}
};

31
test007.cpp Normal file
View File

@@ -0,0 +1,31 @@
class Solution {
public:
int trap(vector<int>& h) {
int n = h.size();
using pii = pair<int, int>;
using ll = long long;
stack<pii> stk;
int ret = 0;
for(int i = 0; i < n; i++){
ll sum = 0;
ll W = 0;
int lastH = 0;
while(!stk.empty() && stk.top().second < h[i]){
lastH = stk.top().second;
W += stk.top().first;
sum += 1LL * stk.top().first * stk.top().second;
stk.pop();
}
if(stk.empty()){
ret += W * lastH - sum;
stk.push({1, h[i]});
}else{
ret += W * h[i] - sum;
stk.push({W + 1, h[i]});
}
}
return ret;
}
};

17
test008.cpp Normal file
View File

@@ -0,0 +1,17 @@
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int n = s.length();
int ret = 0;
unordered_map<char, int> count;
int l = 0;
for(int r = 0; r < n; r++){
count[s[r]]++;
while(count[s[r]] >= 2){
count[s[l++]]--;
}
ret = max(ret, r - l + 1);
}
return ret;
}
};

29
test009.cpp Normal file
View File

@@ -0,0 +1,29 @@
class Solution {
public:
bool checkCnt(vector<int>& cnts, vector<int>& cntp){
for(int i = 0; i < 26; i++){
if(cnts[i] != cntp[i]) return false;
}
return true;
}
vector<int> findAnagrams(string s, string p) {
vector<int> cntp(26);
for(char c:p){ cntp[c-'a']++; }
vector<int> ret;
vector<int> cnts(26);
for(int i = 0; i < min(p.length(), s.length()); i++){
cnts[s[i] - 'a']++;
}
if(checkCnt(cnts, cntp)) { ret.push_back(0);}
for(int r = p.length(); r < s.length(); r++){ // sliding window
int l = r - p.length() + 1;
cnts[s[l-1] - 'a']--;
cnts[s[r] - 'a']++;
if(checkCnt(cnts, cntp)) { ret.push_back(l); }
}
return ret;
}
};

20
test010.cpp Normal file
View File

@@ -0,0 +1,20 @@
class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
int n = nums.size();
vector<int> p(n+1, 0);
for(int i = 1; i <= n; i++){
p[i] = p[i - 1] + nums[i - 1];
}
int ret = 0;
unordered_map<int, int> data;
for(int l = n; l; l--){
data[p[l]]++;
int target = k + p[l-1];
if(data.count(target)){
ret += data[target];
}
}
return ret;
}
};

48
test011.cpp Normal file
View File

@@ -0,0 +1,48 @@
// 方法一、优先队列
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
int n = nums.size();
priority_queue<pair<int, int>> q;
for(int i = 0; i < k; i++){
q.emplace(nums[i], i);
}
vector<int> ans = {q.top().first};
for(int i = k; i < n; i++){
q.emplace(nums[i], i);
while(q.top().second <= (i - k)){
q.pop();
}
ans.push_back(q.top().first);
}
return ans;
}
};
// 方法二、单调队列
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
int n = nums.size();
deque<int> q;
vector<int> ans;
for(int i = 0; i < k; i++){
while(!q.empty() && nums[i] >= nums[q.back()]){
q.pop_back();
}
q.push_back(i);
}
ans.push_back(nums[q.front()]);
for(int i = k; i < n; i++){
while(!q.empty() && nums[i] >= nums[q.back()]){
q.pop_back();
}
q.push_back(i);
while(q.front() <= i - k){
q.pop_front();
}
ans.push_back(nums[q.front()]);
}
return ans;
}
};