Compare commits
15 Commits
c783a5ff9c
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| ba4fe4c9a0 | |||
| f65b2bc248 | |||
| 0740d30bdc | |||
| 124b24c361 | |||
| b0d0b61aa1 | |||
| ab592a9f3d | |||
| 9294aed9b4 | |||
| 56539bc6c1 | |||
| 43c0feea86 | |||
| aad0631b91 | |||
| 1651e74bfa | |||
| f8f0a3632b | |||
| 61531cb43f | |||
| 4144e8d7d3 | |||
| 2d390fedb8 |
16
test002.cpp
Normal file
16
test002.cpp
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<vector<string>> groupAnagrams(vector<string>& strs) {
|
||||||
|
unordered_map<string, vector<string>> data;
|
||||||
|
for(const auto &s : strs){
|
||||||
|
auto key = s;
|
||||||
|
sort(key.begin(), key.end());
|
||||||
|
data[key].push_back(s);
|
||||||
|
}
|
||||||
|
vector<vector<string>> ret;
|
||||||
|
for(const auto &line : data){
|
||||||
|
ret.push_back(line.second);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
};
|
||||||
21
test003.cpp
Normal file
21
test003.cpp
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int longestConsecutive(vector<int>& nums) {
|
||||||
|
unordered_map <int, int> data;
|
||||||
|
for(int x:nums) data[x] = 1;
|
||||||
|
int res = 0;
|
||||||
|
for(auto &n:data){
|
||||||
|
if(n.second){
|
||||||
|
int val = n.first; int len = 1;
|
||||||
|
for(int i = 1; data.count(val - i)&&data[val - i]; i++){
|
||||||
|
data[val - i] = 0; len++;
|
||||||
|
}
|
||||||
|
for(int i = 1; data.count(val + i)&&data[val + i]; i++){
|
||||||
|
data[val + i] = 0; len++;
|
||||||
|
}
|
||||||
|
res = max(len, res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
};
|
||||||
11
test004.cpp
Normal file
11
test004.cpp
Normal 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
16
test005.cpp
Normal 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
28
test006.cpp
Normal 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
31
test007.cpp
Normal 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
17
test008.cpp
Normal 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
29
test009.cpp
Normal 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
20
test010.cpp
Normal 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
48
test011.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user