mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-08-21 18:52:07 +00:00
update
This commit is contained in:
parent
50a8547e70
commit
3433533b24
47
src/8/12/3sum.cpp
Normal file
47
src/8/12/3sum.cpp
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
#include <algorithm>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <functional>
|
||||||
|
#include <iostream>
|
||||||
|
#include <map>
|
||||||
|
#include <set>
|
||||||
|
#include <tuple>
|
||||||
|
#include <unordered_set>
|
||||||
|
#include <vector>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<vector<int>> threeSum(vector<int>& nums) {
|
||||||
|
vector<vector<int>> res;
|
||||||
|
set<vector<int>> s;
|
||||||
|
map<int,int> m;
|
||||||
|
for(int i=0;i<nums.size();i++){
|
||||||
|
m[nums[i]]++;
|
||||||
|
}
|
||||||
|
for(int i=0;i<nums.size();i++){
|
||||||
|
for(int j=i+1;j<nums.size();j++){
|
||||||
|
if(auto p =m.find(-(nums[i]+nums[j]));p!=m.end()){
|
||||||
|
if(nums[i]==nums[j]){
|
||||||
|
auto p = m.find(nums[i]);
|
||||||
|
if(p->second>2)goto ok;
|
||||||
|
}else{
|
||||||
|
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
ok:;
|
||||||
|
vector<int> nn={nums[i],nums[j],-(nums[i]+nums[j])};
|
||||||
|
sort(nn.begin(),nn.end());
|
||||||
|
if(s.find(nn)==s.end()){
|
||||||
|
res.push_back(nn);
|
||||||
|
s.insert(nn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
|
||||||
|
}
|
15
src/8/12/group-anagrams.cpp
Normal file
15
src/8/12/group-anagrams.cpp
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#include<vector>
|
||||||
|
#include<string>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<vector<string>> groupAnagrams(vector<string>& strs) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
|
||||||
|
}
|
34
src/8/12/trapping-rain-water.cpp
Normal file
34
src/8/12/trapping-rain-water.cpp
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
#include <vector>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int trap(std::vector<int> &height) {
|
||||||
|
|
||||||
|
if (height.size() < 3) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int n = height.size();
|
||||||
|
int l = 0, r = n - 1;
|
||||||
|
|
||||||
|
int lmax = 0;
|
||||||
|
int rmax = 0;
|
||||||
|
|
||||||
|
long long totw = 0;
|
||||||
|
while (l < r) {
|
||||||
|
lmax = std::max(lmax, height[l]);
|
||||||
|
rmax = std::max(rmax, height[r]);
|
||||||
|
if (lmax < rmax) {
|
||||||
|
totw += lmax - height[l];
|
||||||
|
l++;
|
||||||
|
} else {
|
||||||
|
totw += rmax - height[r];
|
||||||
|
r--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return totw;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user