From 6aeaa7fda690121f856f953feb61e8322c5479b8 Mon Sep 17 00:00:00 2001 From: carry Date: Thu, 2 Jan 2025 18:57:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=88=E5=81=9A=E4=BA=86=E4=B8=80=E4=BA=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MyCalendar.cpp | 47 +++++++++++++++++++++++++++++++++++++++ rankTeams.cpp | 36 ++++++++++++++++++++++++++++++ threeSum.cpp | 60 ++++++++++++++++++++++++++++++++++++++------------ 3 files changed, 129 insertions(+), 14 deletions(-) create mode 100644 MyCalendar.cpp create mode 100644 rankTeams.cpp diff --git a/MyCalendar.cpp b/MyCalendar.cpp new file mode 100644 index 0000000..a218e40 --- /dev/null +++ b/MyCalendar.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +using namespace std; + +class MyCalendar +{ +public: + vector> 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> 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; +} \ No newline at end of file diff --git a/rankTeams.cpp b/rankTeams.cpp new file mode 100644 index 0000000..05340fa --- /dev/null +++ b/rankTeams.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include + +using namespace std; + +class Solution { +public: + string rankTeams(vector& votes) { + unordered_map list; + for(auto i : votes){ + for(int j = 0;j < i.size();j++){ + list[i[j]] += i.size() - j; + } + } + string res; + vector> temp(list.begin(),list.end()); + stable_sort(temp.begin(),temp.end(),[](pair a,pair b){ + return a.second > b.second; + }); + for(auto i : temp){ + res += i.first; + } + + return res; + + } +}; + +int main(){ + vector votes = {"WXYZ","XYZW"}; + Solution s; + cout << s.rankTeams(votes) << endl; + return 0; +} \ No newline at end of file diff --git a/threeSum.cpp b/threeSum.cpp index 62070d8..6af057d 100644 --- a/threeSum.cpp +++ b/threeSum.cpp @@ -7,25 +7,57 @@ using namespace std; class Solution { public: - vector> threeSum(vector &nums) + vector> threeSum(std::vector &nums) { + vector zeros, positives, negatives; + set> result; - sort(nums.begin(), nums.end()); - set> res; + // 划分元素 + for (int num : nums) { + if (num == 0) { + zeros.push_back(num); + } else if (num > 0) { + positives.push_back(num); + } else { + negatives.push_back(num); + } + } - for(int i = 0; i < nums.size(); i++) { - for(int j = i + 1; j < nums.size(); j++) { - for(int k = nums.size() - 1; k > j ; k--) { - if(nums[i] + nums[j] + nums[k] == 0) { - vector tmp = {nums[i], nums[j], nums[k]}; - 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>(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>(result.begin(), result.end()); } }; @@ -34,8 +66,8 @@ int main() Solution s; vector nums = {-1, 0, 1, 2, -1, -4}; vector> res = s.threeSum(nums); - for(auto &v : res) { - for(auto &i : v) { + for (auto &v : res) { + for (auto &i : v) { cout << i << " "; } cout << endl;