From 49620abd121e551abc4fcfcbcbc2a6179fee2b58 Mon Sep 17 00:00:00 2001 From: binacs Date: Sat, 1 Jun 2024 19:54:53 +0800 Subject: [PATCH] add: papers and resources --- basic/binary.md | 38 ++++++++++++++++++++------------------ math/newton.md | 4 ++-- papers.md | 2 ++ resources.md | 20 ++++++++++++++++---- topic/simulation.md | 40 ++++++++++++++++++++++++++++++++++++++++ topic/trick.md | 26 +++++++++++++++++++++++++- 6 files changed, 105 insertions(+), 25 deletions(-) diff --git a/basic/binary.md b/basic/binary.md index a2e8bca..93d1856 100644 --- a/basic/binary.md +++ b/basic/binary.md @@ -448,26 +448,28 @@ class Solution { public: double findMedianSortedArrays(vector& nums1, vector& nums2) { int n1 = nums1.size(), n2 = nums2.size(); - if (n1 > n2) return findMedianSortedArrays(nums2, nums1); - // l 是 nums1 中划分点的右边界 取值范围 [0, n1] - // 对应:l 左侧的值为 [空, n1-1] - // 也即:l 左侧的值 <= 中点值 + if (n1 > n2) + return findMedianSortedArrays(nums2, nums1); + + // 找到一个左半边的结束位置, 使得两边有严格大小关系 int l = 0, r = n1; - // 找到第一个大于等于 lv2 的位置 l while (l < r) { - // 左侧有 i 个 总共应有(n1+n2)/2个 - int i = l + (r - l) / 2; - // nums2 右边界 因为n1和n2大小关系 j永远不会取0(除非n1=n2) - int j = (n1 + n2) / 2 - i; - if (nums1[i] < nums2[j-1]) l = i + 1; // rv1 和 lv2 对比 nums1 选的太少 - else r = i; - } - int i = l, j = (n1 + n2) / 2 - i; - int lv1 = i ? nums1[i - 1] : INT_MIN; - int rv1 = i < n1 ? nums1[i] : INT_MAX; - int lv2 = j ? nums2[j - 1] : INT_MIN; - int rv2 = j < n2 ? nums2[j] : INT_MAX; - if((n1 + n2) & 1) return min(rv1, rv2); + int mid = l + (r - l) / 2; + int l1 = mid; + int l2 = (n1 + n2) / 2 - l1; + if (nums1[l1] < nums2[l2 - 1]) // ATTENTION 个数 -> 下标 偏移 + l = mid + 1; + else + r = mid; + } + + int l1 = l, l2 = (n1 + n2) / 2 - l1; + int lv1 = l1 ? nums1[l1 - 1] : INT_MIN; + int rv1 = l1 < n1 ? nums1[l1] : INT_MAX; + int lv2 = l2 ? nums2[l2 - 1] : INT_MIN; + int rv2 = l2 < n2 ? nums2[l2] : INT_MAX; + if ((n1 + n2) & 1) + return min(rv1, rv2); return (max(lv1, lv2) + min(rv1, rv2)) / 2.0; } }; diff --git a/math/newton.md b/math/newton.md index 13d5655..2efc9d9 100644 --- a/math/newton.md +++ b/math/newton.md @@ -264,7 +264,7 @@ class Solution: 详细代码 -##### **C++** +##### **C++ 1** ```cpp class Solution { @@ -278,7 +278,7 @@ public: }; ``` -##### **C++** +##### **C++ 2** ```cpp class Solution { diff --git a/papers.md b/papers.md index 11d9402..560dcb3 100644 --- a/papers.md +++ b/papers.md @@ -1,3 +1,5 @@ # Papers +[github.com/binacs/paper](https://github.com/binacs/paper) + [github.com/binacs/blog](https://github.com/binacs/blog) \ No newline at end of file diff --git a/resources.md b/resources.md index 264c562..24c0648 100644 --- a/resources.md +++ b/resources.md @@ -18,22 +18,34 @@ ## English -[Chinese Programmer Wrong Pronunciation](https://cpwp.netlify.app) - -> https://github.com/antfu/cpwp +### Words [most-frequent-technology-english-words](https://learn-english.dev) > https://github.com/Wei-Xia/most-frequent-technology-english-words -[English-level-up-tips](https://github.com/byoungd/English-level-up-tips) +[Chinese Programmer Wrong Pronunciation](https://cpwp.netlify.app) + +> https://github.com/antfu/cpwp [interview-english](https://github.com/Interview-Science/interview-english) +### IELTS + +[Salvation lies within IELTS](https://hefengxian.github.io/my-ielts/#/) + +> https://github.com/hefengxian/my-ielts + +### Type Training + [Qwerty Learner](https://qwerty.kaiyi.cool/) [Eye Types](https://www.eletypes.com/) +### Others + +[English-level-up-tips](https://github.com/byoungd/English-level-up-tips) + [A-Programmers-Guide-to-English](https://github.com/yujiangshui/A-Programmers-Guide-to-English) ## Quant diff --git a/topic/simulation.md b/topic/simulation.md index 6748bfa..2c3ccbe 100644 --- a/topic/simulation.md +++ b/topic/simulation.md @@ -5122,6 +5122,46 @@ class Solution: 详细代码 +##### **C++ latest** + +```cpp +class Solution { +public: + int divide(int x, int y) { + // ATTENTION 最小剪枝 + if (x == INT_MIN && y == -1) + return INT_MAX; + + bool is_minus = false; + if (x < 0 && y > 0 || x > 0 && y < 0) is_minus = true; + int a = x < 0 ? x : -x, b = y < 0 ? y : -y; + + vector exp; + // for (int i = b; i >= a; i = i + i) exp.push_back(i); + for (int i = b; i >= a; i = i + i) { + exp.push_back(i); + if (i < a / 2) // ATTENTION: pre check to void runtime error + break; + } + + int res = 0; + for (int i = exp.size() - 1; i >= 0; i -- ) + if (a <= exp[i]) { + a -= exp[i]; // 负数 所以同样要减 + res += 1ll << i; + } + + if (is_minus) { + if (res == INT_MIN) + return INT_MIN; // ATTENTION 细节 意味着 INT_MIN/1 + return -res; + } + return res; + } +}; +``` + + ##### **C++ 1** ```cpp diff --git a/topic/trick.md b/topic/trick.md index 7ba303e..8af6173 100644 --- a/topic/trick.md +++ b/topic/trick.md @@ -909,7 +909,31 @@ class Solution: 详细代码 -##### **C++** +##### **C++ 1** + +```cpp +class Solution { +public: + int longestConsecutive(vector& nums) { + int n = nums.size(); + unordered_map m; + for (auto v : nums) + m[v] = true; + int res = 0; + for (auto v : nums) { + if (m[v - 1]) + continue; + int cnt = 1; + while (m[ ++ v]) + cnt ++ ; + res = max(res, cnt); + } + return res; + } +}; +``` + +##### **C++ 2** ```cpp class Solution {