又做了一些

This commit is contained in:
carry 2025-01-02 18:57:27 +08:00
parent 759c5b09ce
commit 6aeaa7fda6
3 changed files with 129 additions and 14 deletions

47
MyCalendar.cpp Normal file
View File

@ -0,0 +1,47 @@
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
class MyCalendar
{
public:
vector<std::pair<int, int>> cal;
MyCalendar()
{
}
bool book(int startTime, int endTime)
{
for (auto i : this->cal) {
if ((i.first <= startTime) and (i.second > startTime)) {
return false;
}
if ((i.first < endTime) and (i.second >= endTime)) {
return false;
}
if ((i.first <= startTime) and (i.second >= endTime)) {
return false;
}
if ((i.first >= startTime) and (i.second <= endTime)) {
return false;
}
}
this->cal.push_back(pair(startTime, endTime));
return true;
}
};
int main()
{
vector<pair<int, int>> to_try = {{97, 100}, {33, 51}, {89, 100}, {83, 100}, {75, 92}, {76, 95}, {19, 30}, {53, 63}, {8, 23}, {18, 37}, {87, 100}, {83, 100}, {54, 67}, {35, 48}, {58, 75}, {70, 89}, {13, 32}, {44, 63}, {51, 62}, {2, 15}};
MyCalendar *obj = new MyCalendar();
for (auto i : to_try) {
if (obj->book(i.first, i.second)) {
cout << "true " << i.first << " " << i.second << endl;
} else {
cout << "false " << i.first << " " << i.second << endl;
}
}
return 0;
}

36
rankTeams.cpp Normal file
View File

@ -0,0 +1,36 @@
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
class Solution {
public:
string rankTeams(vector<string>& votes) {
unordered_map<char,int> list;
for(auto i : votes){
for(int j = 0;j < i.size();j++){
list[i[j]] += i.size() - j;
}
}
string res;
vector<pair<char,int>> temp(list.begin(),list.end());
stable_sort(temp.begin(),temp.end(),[](pair<char,int> a,pair<char,int> b){
return a.second > b.second;
});
for(auto i : temp){
res += i.first;
}
return res;
}
};
int main(){
vector<string> votes = {"WXYZ","XYZW"};
Solution s;
cout << s.rankTeams(votes) << endl;
return 0;
}

View File

@ -7,25 +7,57 @@ using namespace std;
class Solution class Solution
{ {
public: public:
vector<vector<int>> threeSum(vector<int> &nums) vector<vector<int>> threeSum(std::vector<int> &nums)
{ {
vector<int> zeros, positives, negatives;
set<vector<int>> result;
sort(nums.begin(), nums.end()); // 划分元素
set<vector<int>> res; for (int num : nums) {
if (num == 0) {
for(int i = 0; i < nums.size(); i++) { zeros.push_back(num);
for(int j = i + 1; j < nums.size(); j++) { } else if (num > 0) {
for(int k = nums.size() - 1; k > j ; k--) { positives.push_back(num);
if(nums[i] + nums[j] + nums[k] == 0) { } else {
vector<int> tmp = {nums[i], nums[j], nums[k]}; negatives.push_back(num);
res.insert(tmp); }
} }
// 排序正数和负数
std::sort(positives.begin(), positives.end());
std::sort(negatives.begin(), negatives.end());
if (zeros.size() != 0) {
if (zeros.size() >= 3) {
result.insert({0, 0, 0});
}
for (int positive : positives) {
if (std::binary_search(negatives.begin(), negatives.end(), -positive)) {
result.insert({-positive, 0, positive});
} }
} }
} }
return vector<vector<int>>(res.begin(), res.end()); //正正负
for(int i = 0; i < positives.size(); i++) {
for(int j = i + 1; j < positives.size(); j++) {
if(std::binary_search(negatives.begin(), negatives.end(), -(positives[i] + positives[j]))){
result.insert({-(positives[i] + positives[j]), positives[i], positives[j]});
}
}
}
//负负正
for(int i = 0; i < negatives.size(); i++) {
for(int j = i + 1; j < negatives.size(); j++) {
if (std::binary_search(positives.begin(), positives.end(), -(negatives[i] + negatives[j]))) {
result.insert({negatives[i], negatives[j], -(negatives[i] + negatives[j])});
}
}
}
return vector<vector<int>>(result.begin(), result.end());
} }
}; };
@ -34,8 +66,8 @@ int main()
Solution s; Solution s;
vector<int> nums = {-1, 0, 1, 2, -1, -4}; vector<int> nums = {-1, 0, 1, 2, -1, -4};
vector<vector<int>> res = s.threeSum(nums); vector<vector<int>> res = s.threeSum(nums);
for(auto &v : res) { for (auto &v : res) {
for(auto &i : v) { for (auto &i : v) {
cout << i << " "; cout << i << " ";
} }
cout << endl; cout << endl;