This commit is contained in:
Zengtudor 2025-08-12 20:42:13 +08:00
parent 50a8547e70
commit 3433533b24
3 changed files with 96 additions and 0 deletions

47
src/8/12/3sum.cpp Normal file
View 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(){
}

View File

@ -0,0 +1,15 @@
#include<vector>
#include<string>
using namespace std;
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
}
};
int main(){
}

View 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() {
}