From 03103f211e2dd01b2976f526d933fc88fe88a55f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E4=B8=89?= <15933693+three-three33@user.noreply.gitee.com> Date: Tue, 14 Oct 2025 19:09:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=93=E9=A2=98=E4=BA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ..._\351\202\223\345\256\266\345\205\264.cpp" | 23 ++++++++++++ ..._\351\202\223\345\256\266\345\205\264.cpp" | 33 +++++++++++++++++ ..._\351\202\223\345\256\266\345\205\264.cpp" | 26 +++++++++++++ ..._\351\202\223\345\256\266\345\205\264.cpp" | 37 +++++++++++++++++++ ..._\351\202\223\345\256\266\345\205\264.cpp" | 29 +++++++++++++++ ..._\351\202\223\345\256\266\345\205\264.cpp" | 20 ++++++++++ 6 files changed, 168 insertions(+) create mode 100644 "topic02/submit/LC0540_\351\202\223\345\256\266\345\205\264.cpp" create mode 100644 "topic02/submit/LC0611_\351\202\223\345\256\266\345\205\264.cpp" create mode 100644 "topic02/submit/LC0704_\351\202\223\345\256\266\345\205\264.cpp" create mode 100644 "topic02/submit/LC1337_\351\202\223\345\256\266\345\205\264.cpp" create mode 100644 "topic02/submit/LC2529_\351\202\223\345\256\266\345\205\264.cpp" create mode 100644 "topic02/submit/LC2824_\351\202\223\345\256\266\345\205\264.cpp" diff --git "a/topic02/submit/LC0540_\351\202\223\345\256\266\345\205\264.cpp" "b/topic02/submit/LC0540_\351\202\223\345\256\266\345\205\264.cpp" new file mode 100644 index 0000000..ce9d27e --- /dev/null +++ "b/topic02/submit/LC0540_\351\202\223\345\256\266\345\205\264.cpp" @@ -0,0 +1,23 @@ +#include +using namespace std; + +// 【题目】力扣.0540 有序数组中的单一元素 +// 【难度】中等 +// 【提交】https://leetcode.cn/problems/single-element-in-a-sorted-array/submissions/669079320/?envType=problem-list-v2&envId=binary-search +// 【标签】数组;二分查找 + +class Solution { +public: + int singleNonDuplicate(vector& nums) { + int low = 0, high = nums.size() - 1; + while (low < high) { + int mid = (high - low) / 2 + low; + if (nums[mid] == nums[mid ^ 1]) { + low = mid + 1; + } else { + high = mid; + } + } + return nums[low]; + } +}; diff --git "a/topic02/submit/LC0611_\351\202\223\345\256\266\345\205\264.cpp" "b/topic02/submit/LC0611_\351\202\223\345\256\266\345\205\264.cpp" new file mode 100644 index 0000000..69e390b --- /dev/null +++ "b/topic02/submit/LC0611_\351\202\223\345\256\266\345\205\264.cpp" @@ -0,0 +1,33 @@ +#include +using namespace std; + +// 【题目】力扣0611.有效三角形的个数 +// 【难度】中等 +// 【提交】https://leetcode.cn/problems/valid-triangle-number/submissions/666775712/?envType=problem-list-v2&envId=binary-search +// 【标签】贪心;数组;双指针;二分查找;排序 + +class Solution { +public: + int triangleNumber(vector& nums) { + int n = nums.size(); + sort(nums.begin(), nums.end()); + int ans = 0; + for (int i = 0; i < n; ++i) { + for (int j = i + 1; j < n; ++j) { + int left = j + 1, right = n - 1, k = j; + while (left <= right) { + int mid = (left + right) / 2; + if (nums[mid] < nums[i] + nums[j]) { + k = mid; + left = mid + 1; + } + else { + right = mid - 1; + } + } + ans += k - j; + } + } + return ans; + } +}; diff --git "a/topic02/submit/LC0704_\351\202\223\345\256\266\345\205\264.cpp" "b/topic02/submit/LC0704_\351\202\223\345\256\266\345\205\264.cpp" new file mode 100644 index 0000000..32454d6 --- /dev/null +++ "b/topic02/submit/LC0704_\351\202\223\345\256\266\345\205\264.cpp" @@ -0,0 +1,26 @@ +#include +using namespace std; + +// 【题目】力扣0704.二分查找 +// 【难度】简单 +// 【提交】https://leetcode.cn/problems/binary-search/submissions/669072794/?envType=problem-list-v2&envId=binary-search +// 【标签】数组;二分查找 + +class Solution { +public: + int search(vector& nums, int target) { + int left = 0, right = nums.size() - 1; + while (left <= right) { + int mid = (right + left) / 2; + int num = nums[mid]; + if (num == target) { + return mid; + } else if (num > target) { + right = mid - 1; + } else (num < target){ + left = mid + 1; + } + } + return -1; + } +}; \ No newline at end of file diff --git "a/topic02/submit/LC1337_\351\202\223\345\256\266\345\205\264.cpp" "b/topic02/submit/LC1337_\351\202\223\345\256\266\345\205\264.cpp" new file mode 100644 index 0000000..3c94327 --- /dev/null +++ "b/topic02/submit/LC1337_\351\202\223\345\256\266\345\205\264.cpp" @@ -0,0 +1,37 @@ +#include +using namespace std; + +// 【题目】力扣1337.矩阵中战斗力最弱的K行 +// 【难度】简单 +// 【提交】https://leetcode.cn/problems/the-k-weakest-rows-in-a-matrix/submissions/669084888/?envType=problem-list-v2&envId=binary-search +// 【标签】数组;二分查找;矩阵;排序;堆(优先队列) + +class Solution { +public: + vector kWeakestRows(vector>& mat, int k) { + int m = mat.size(), n = mat[0].size(); + vector> power; + for (int i = 0; i < m; ++i) { + int l = 0, r = n - 1, pos = -1; + while (l <= r) { + int mid = (l + r) / 2; + if (mat[i][mid] == 0) { + r = mid - 1; + } + else { + pos = mid; + l = mid + 1; + } + } + power.emplace_back(pos + 1, i); + } + + priority_queue q(greater>(), move(power)); + vector ans; + for (int i = 0; i < k; ++i) { + ans.push_back(q.top().second); + q.pop(); + } + return ans; + } +}; diff --git "a/topic02/submit/LC2529_\351\202\223\345\256\266\345\205\264.cpp" "b/topic02/submit/LC2529_\351\202\223\345\256\266\345\205\264.cpp" new file mode 100644 index 0000000..12ab482 --- /dev/null +++ "b/topic02/submit/LC2529_\351\202\223\345\256\266\345\205\264.cpp" @@ -0,0 +1,29 @@ +#include +using namespace std; + +// 【题目】力扣2529.正整数和负整数的最大计数 +// 【难度】简单 +// 【提交】https://leetcode.cn/problems/maximum-count-of-positive-integer-and-negative-integer/submissions/669087408/?envType=problem-list-v2&envId=binary-search +// 【标签】数组;二分查找;计数 + +class Solution { +public: + int lowerBound(vector& nums, int val) { + int l = 0, r = nums.size(); + while (l < r) { + int m = (l + r) / 2; + if (nums[m] >= val) { + r = m; + } else { + l = m + 1; + } + } + return l; + } + + int maximumCount(vector& nums) { + int pos1 = lowerBound(nums, 0); + int pos2 = lowerBound(nums, 1); + return max(pos1, (int) nums.size() - pos2); + } +}; diff --git "a/topic02/submit/LC2824_\351\202\223\345\256\266\345\205\264.cpp" "b/topic02/submit/LC2824_\351\202\223\345\256\266\345\205\264.cpp" new file mode 100644 index 0000000..533fd26 --- /dev/null +++ "b/topic02/submit/LC2824_\351\202\223\345\256\266\345\205\264.cpp" @@ -0,0 +1,20 @@ +#include +using namespace std; + +// 【题目】力扣2824.统计和小于目标的下标对数目 +// 【难度】简单 +// 【提交】https://leetcode.cn/problems/count-pairs-whose-sum-is-less-than-target/submissions/669091124/?envType=problem-list-v2&envId=binary-search +// 【标签】数组;双指针;二分查找;排序 + +class Solution { +public: + int countPairs(vector& nums, int target) { + sort(nums.begin(), nums.end()); + int res = 0; + for (int i = 1; i < nums.size(); i++) { + int k = lower_bound(nums.begin(), nums.begin() + i, target - nums[i]) - nums.begin(); + res += k; + } + return res; + } +}; -- Gitee