{"id":79377,"date":"2026-03-01T22:49:08","date_gmt":"2026-03-01T14:49:08","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/79377.html"},"modified":"2026-03-01T22:49:08","modified_gmt":"2026-03-01T14:49:08","slug":"%e3%80%8aleetcode-%e9%a1%ba%e5%ba%8f%e5%88%b7%e9%a2%98%e3%80%8b21-30","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/79377.html","title":{"rendered":"\u300aLeetCode \u987a\u5e8f\u5237\u9898\u300b21 - 30"},"content":{"rendered":"<h4>21\u3001[\u7b80\u5355] \u5408\u5e76\u4e24\u4e2a\u6709\u5e8f\u94fe\u8868<\/h4>\n<p>\u9012\u5f52 \u94fe\u8868<\/p>\n<p>\u8fed\u4ee3<\/p>\n<p>class Solution {<br \/>\npublic:<br \/>\n    ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {<br \/>\n        ListNode head;<br \/>\n        ListNode *cur &#061; &amp;head, *cur1 &#061; list1, *cur2 &#061; list2;<br \/>\n        while (cur1 &amp;&amp; cur2) {<br \/>\n            if (cur1-&gt;val &lt; cur2-&gt;val) {<br \/>\n                cur-&gt;next &#061; cur1;<br \/>\n                cur1 &#061; cur1-&gt;next;<br \/>\n            } else {<br \/>\n                cur-&gt;next &#061; cur2;<br \/>\n                cur2 &#061; cur2-&gt;next;<br \/>\n            }<br \/>\n            cur &#061; cur-&gt;next;<br \/>\n        }<br \/>\n        if (cur1) {<br \/>\n            cur-&gt;next &#061; cur1;<br \/>\n        } else {<br \/>\n            cur-&gt;next &#061; cur2;<br \/>\n        }<br \/>\n        return head.next;<br \/>\n    }<br \/>\n};<\/p>\n<p>\u9012\u5f52<\/p>\n<p>class Solution {<br \/>\npublic:<br \/>\n    ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {<br \/>\n        if (list1 &#061;&#061; nullptr) {<br \/>\n            return list2;<br \/>\n        } else if (list2 &#061;&#061; nullptr) {<br \/>\n            return list1;<br \/>\n        } else if (list1-&gt;val &lt; list2-&gt;val) {<br \/>\n            list1-&gt;next &#061; mergeTwoLists(list1-&gt;next, list2);<br \/>\n            return list1;<br \/>\n        } else {<br \/>\n            list2-&gt;next &#061; mergeTwoLists(list1, list2-&gt;next);<br \/>\n            return list2;<br \/>\n        }<br \/>\n    }<br \/>\n};<\/p>\n<h4>22\u3001[\u4e2d\u7b49] \u62ec\u53f7\u751f\u6210<\/h4>\n<p>\u5b57\u7b26\u4e32 \u56de\u6eaf<\/p>\n<p>class Solution {<br \/>\npublic:<br \/>\n    vector&lt;string&gt; generateParenthesis(int n) {<br \/>\n        vector&lt;string&gt; ret;<br \/>\n        string path;<br \/>\n        int left &#061; 0, right &#061; 0;<br \/>\n        dfs(ret, path, left, right, n);<br \/>\n        return ret;<br \/>\n    }<\/p>\n<p>    void dfs(vector&lt;string&gt;&amp; ret, string&amp; path, int left, int right, int n) {<br \/>\n        if (right &#061;&#061; n) {<br \/>\n            ret.push_back(path);<br \/>\n            return;<br \/>\n        }<br \/>\n        if (left &lt; n) {<br \/>\n            path.push_back(&#039;(&#039;);<br \/>\n            dfs(ret, path, left &#043; 1, right, n);<br \/>\n            path.pop_back();<br \/>\n        }<br \/>\n        if (right &lt; left) {<br \/>\n            path.push_back(&#039;)&#039;);<br \/>\n            dfs(ret, path, left, right &#043; 1, n);<br \/>\n            path.pop_back();<br \/>\n        }<br \/>\n    }<br \/>\n};<\/p>\n<h4>23\u3001[\u56f0\u96be] \u5408\u5e76 K \u4e2a\u5347\u5e8f\u94fe\u8868<\/h4>\n<p>\u94fe\u8868 \u5206\u6cbb<\/p>\n<p>\u987a\u5e8f\u5408\u5e76<\/p>\n<p>class Solution {<br \/>\npublic:<br \/>\n    ListNode* mergeKLists(vector&lt;ListNode*&gt;&amp; lists) {<br \/>\n        ListNode* ret &#061; nullptr;<br \/>\n        for (size_t i &#061; 0; i &lt; lists.size(); i&#043;&#043;) {<br \/>\n            ret &#061; mergeTwoLists(ret, lists[i]);<br \/>\n        }<br \/>\n        return ret;<br \/>\n    }<\/p>\n<p>    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {<br \/>\n        if (!l1 || !l2) {<br \/>\n            return l1 ? l1 : l2;<br \/>\n        }<\/p>\n<p>        ListNode head, *cur &#061; &amp;head, *cur1 &#061; l1, *cur2 &#061; l2;<br \/>\n        while (cur1 &amp;&amp; cur2) {<br \/>\n            if (cur1-&gt;val &lt; cur2-&gt;val) {<br \/>\n                cur-&gt;next &#061; cur1;<br \/>\n                cur1 &#061; cur1-&gt;next;<br \/>\n            } else {<br \/>\n                cur-&gt;next &#061; cur2;<br \/>\n                cur2 &#061; cur2-&gt;next;<br \/>\n            }<br \/>\n            cur &#061; cur-&gt;next;<br \/>\n        }<\/p>\n<p>        cur-&gt;next &#061; cur1 ? cur1 : cur2;<br \/>\n        return head.next;<br \/>\n    }<br \/>\n};<\/p>\n<p>\u5408\u5e76\u5206\u6cbb<\/p>\n<p>class Solution {<br \/>\npublic:<br \/>\n    ListNode* mergeKLists(vector&lt;ListNode*&gt;&amp; lists) {<br \/>\n        return merge(lists, 0, lists.size() &#8211; 1);<br \/>\n    }<\/p>\n<p>    ListNode* merge(vector&lt;ListNode*&gt;&amp; lists, int left, int right) {<br \/>\n        if (left &gt; right) {<br \/>\n            return nullptr;<br \/>\n        }<br \/>\n        if (left &#061;&#061; right) {<br \/>\n            return lists[left];<br \/>\n        }<br \/>\n        int mid &#061; (left &#043; right) &gt;&gt; 1;<br \/>\n        return mergeTwoLists(merge(lists, left, mid), merge(lists, mid &#043; 1, right));<br \/>\n    }<\/p>\n<p>    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {<br \/>\n        if (!l1 || !l2) {<br \/>\n            return l1 ? l1 : l2;<br \/>\n        }<\/p>\n<p>        ListNode head, *cur &#061; &amp;head, *cur1 &#061; l1, *cur2 &#061; l2;<br \/>\n        while (cur1 &amp;&amp; cur2) {<br \/>\n            if (cur1-&gt;val &lt; cur2-&gt;val) {<br \/>\n                cur-&gt;next &#061; cur1;<br \/>\n                cur1 &#061; cur1-&gt;next;<br \/>\n            } else {<br \/>\n                cur-&gt;next &#061; cur2;<br \/>\n                cur2 &#061; cur2-&gt;next;<br \/>\n            }<br \/>\n            cur &#061; cur-&gt;next;<br \/>\n        }<\/p>\n<p>        cur-&gt;next &#061; cur1 ? cur1 : cur2;<br \/>\n        return head.next;<br \/>\n    }<br \/>\n};<\/p>\n<p>\u4f18\u5148\u7ea7\u961f\u5217<\/p>\n<p>struct Status {<br \/>\n    int val;<br \/>\n    ListNode* ptr;<\/p>\n<p>    bool operator&lt;(const Status&amp; rhs) const { return val &gt; rhs.val; }<\/p>\n<p>    Status(int x, ListNode* node) : val(x), ptr(node) {}<br \/>\n};<\/p>\n<p>class Solution {<br \/>\npublic:<br \/>\n    ListNode* mergeKLists(vector&lt;ListNode*&gt;&amp; lists) {<br \/>\n        priority_queue&lt;Status&gt; q;<br \/>\n        for (const auto&amp; node : lists) {<br \/>\n            if (node) {<br \/>\n                q.push({node-&gt;val, node});<br \/>\n            }<br \/>\n        }<br \/>\n        ListNode head, *cur &#061; &amp;head;<br \/>\n        while (!q.empty()) {<br \/>\n            auto front &#061; q.top();<br \/>\n            q.pop();<br \/>\n            cur-&gt;next &#061; front.ptr;<br \/>\n            cur &#061; cur-&gt;next;<br \/>\n            if (front.ptr-&gt;next) {<br \/>\n                q.push({front.ptr-&gt;next-&gt;val, front.ptr-&gt;next});<br \/>\n            }<br \/>\n        }<br \/>\n        return head.next;<br \/>\n    }<br \/>\n};<\/p>\n<h4>24\u3001[\u4e2d\u7b49] \u4e24\u4e24\u4ea4\u6362\u94fe\u8868\u4e2d\u7684\u8282\u70b9<\/h4>\n<p>\u94fe\u8868<\/p>\n<p>\u9012\u5f52<\/p>\n<p>class Solution {<br \/>\npublic:<br \/>\n    ListNode* swapPairs(ListNode* head) {<br \/>\n        if (head &#061;&#061; nullptr || head-&gt;next &#061;&#061; nullptr) {<br \/>\n            return head;<br \/>\n        }<\/p>\n<p>        ListNode* newhead &#061; head-&gt;next;<br \/>\n        head-&gt;next &#061; swapPairs(newhead-&gt;next);<br \/>\n        newhead-&gt;next &#061; head;<\/p>\n<p>        return newhead;<br \/>\n    }<br \/>\n};<\/p>\n<p>\u8fed\u4ee3<\/p>\n<p>class Solution {<br \/>\npublic:<br \/>\n    ListNode* swapPairs(ListNode* head) {<br \/>\n        ListNode newhead;<br \/>\n        newhead.next &#061; head;<br \/>\n        ListNode* cur &#061; &amp;newhead;<br \/>\n        while (cur-&gt;next &amp;&amp; cur-&gt;next-&gt;next) {<br \/>\n            ListNode* node1 &#061; cur-&gt;next;<br \/>\n            ListNode* node2 &#061; cur-&gt;next-&gt;next;<br \/>\n            cur-&gt;next &#061; node2;<br \/>\n            node1-&gt;next &#061; node2-&gt;next;<br \/>\n            node2-&gt;next &#061; node1;<br \/>\n            cur &#061; node1;<br \/>\n        }<br \/>\n        return newhead.next;<br \/>\n    }<br \/>\n};<\/p>\n<p>class Solution {<br \/>\npublic:<br \/>\n    ListNode* swapPairs(ListNode* head) {<br \/>\n        if (head &#061;&#061; nullptr || head-&gt;next &#061;&#061; nullptr) {<br \/>\n            return head;<br \/>\n        }<\/p>\n<p>        ListNode newhead;<br \/>\n        newhead.next &#061; head;<br \/>\n        ListNode* prev &#061; &amp;newhead;<br \/>\n        ListNode* cur &#061; prev-&gt;next;<br \/>\n        ListNode* next &#061; cur-&gt;next;<\/p>\n<p>        while (cur &amp;&amp; next) {<br \/>\n            prev-&gt;next &#061; next;<br \/>\n            cur-&gt;next &#061; next-&gt;next;<br \/>\n            next-&gt;next &#061; cur;<\/p>\n<p>            prev &#061; cur;<br \/>\n            cur &#061; cur-&gt;next;<br \/>\n            if (cur) {<br \/>\n                next &#061; cur-&gt;next;<br \/>\n            }<br \/>\n        }<\/p>\n<p>        return newhead.next;<br \/>\n    }<br \/>\n};<\/p>\n<h4>25\u3001[\u56f0\u96be] K \u4e2a\u4e00\u7ec4\u7ffb\u8f6c\u94fe\u8868<\/h4>\n<p>\u94fe\u8868<\/p>\n<p>class Solution {<br \/>\npublic:<br \/>\n    ListNode* reverseKGroup(ListNode* head, int k) {<br \/>\n        \/\/ 1. \u5148\u6c42\u51fa\u9700\u8981\u9006\u5e8f\u591a\u5c11\u7ec4<br \/>\n        int n &#061; 0;<br \/>\n        ListNode* cur &#061; head;<br \/>\n        while (cur) {<br \/>\n            cur &#061; cur-&gt;next;<br \/>\n            n&#043;&#043;;<br \/>\n        }<br \/>\n        n \/&#061; k;<\/p>\n<p>        \/\/ 2. \u91cd\u590d n \u6b21&#xff1a;\u957f\u5ea6\u4e3a k \u7684\u94fe\u8868\u7684\u9006\u5e8f\u5373\u53ef<br \/>\n        ListNode newhead;<br \/>\n        ListNode* pre &#061; &amp;newhead;<br \/>\n        cur &#061; head;<br \/>\n        for (int i &#061; 0; i &lt; n; i&#043;&#043;) {<br \/>\n            ListNode* tmp &#061; cur;<br \/>\n            for (int j &#061; 0; j &lt; k; j&#043;&#043;) {<br \/>\n                ListNode* next &#061; cur-&gt;next;<br \/>\n                cur-&gt;next &#061; pre-&gt;next;<br \/>\n                pre-&gt;next &#061; cur;<br \/>\n                cur &#061; next;<br \/>\n            }<br \/>\n            pre &#061; tmp;<br \/>\n        }<\/p>\n<p>        \/\/ 3. \u628a\u4e0d\u9700\u8981\u7ffb\u8f6c\u7684\u63a5\u4e0a<br \/>\n        pre-&gt;next &#061; cur;<br \/>\n        return newhead.next;<br \/>\n    }<br \/>\n};<\/p>\n<h4>26\u3001[\u7b80\u5355] \u5220\u9664\u6709\u5e8f\u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u9879<\/h4>\n<p>\u6570\u7ec4 \u53cc\u6307\u9488<\/p>\n<p>\u53cc\u6307\u9488<\/p>\n<p>class Solution {<br \/>\npublic:<br \/>\n    int removeDuplicates(vector&lt;int&gt;&amp; nums) {<br \/>\n        int n &#061; nums.size();<br \/>\n        if (n &#061;&#061; 0)<br \/>\n            return 0;<\/p>\n<p>        int i &#061; 0, j &#061; 1;<br \/>\n        int dst &#061; 0;<br \/>\n        while (j &lt; n) {<br \/>\n            if (nums[i] &#061;&#061; nums[j]) {<br \/>\n                j&#043;&#043;;<br \/>\n            } else {<br \/>\n                nums[dst&#043;&#043;] &#061; nums[i];<br \/>\n                i &#061; j;<br \/>\n                j&#043;&#043;;<br \/>\n            }<br \/>\n        }<br \/>\n        nums[dst&#043;&#043;] &#061; nums[i];<\/p>\n<p>        return dst;<br \/>\n    }<br \/>\n};<\/p>\n<p>class Solution {<br \/>\npublic:<br \/>\n    int removeDuplicates(vector&lt;int&gt;&amp; nums) {<br \/>\n        int n &#061; nums.size();<br \/>\n        if (n &#061;&#061; 0)<br \/>\n            return 0;<\/p>\n<p>        int fast &#061; 1, slow &#061; 1;<br \/>\n        while (fast &lt; n) {<br \/>\n            if (nums[fast] !&#061; nums[fast &#8211; 1]) {<br \/>\n                nums[slow&#043;&#043;] &#061; nums[fast];<br \/>\n            }<br \/>\n            &#043;&#043;fast;<br \/>\n        }<\/p>\n<p>        return slow;<br \/>\n    }<br \/>\n};<\/p>\n<h4>27\u3001[\u7b80\u5355] \u79fb\u9664\u5143\u7d20<\/h4>\n<p>\u6570\u7ec4 \u53cc\u6307\u9488<\/p>\n<p>\u53cc\u6307\u9488<\/p>\n<p>class Solution {<br \/>\npublic:<br \/>\n    int removeElement(vector&lt;int&gt;&amp; nums, int val) {<br \/>\n        int n &#061; nums.size();<br \/>\n        int src &#061; 0, dst &#061; 0;<br \/>\n        while (src &lt; n) {<br \/>\n            if (nums[src] !&#061; val)<br \/>\n                nums[dst&#043;&#043;] &#061; nums[src&#043;&#043;];<br \/>\n            else<br \/>\n                &#043;&#043;src;<br \/>\n        }<br \/>\n        return dst;<br \/>\n    }<br \/>\n};<\/p>\n<p>\u53cc\u6307\u9488\u4f18\u5316<\/p>\n<p>class Solution {<br \/>\npublic:<br \/>\n    int removeElement(vector&lt;int&gt;&amp; nums, int val) {<br \/>\n        int left &#061; 0, right &#061; nums.size();<br \/>\n        while (left &lt; right) {<br \/>\n            if (nums[left] &#061;&#061; val) {<br \/>\n                nums[left] &#061; nums[&#8211;right];<br \/>\n            } else {<br \/>\n                left&#043;&#043;;<br \/>\n            }<br \/>\n        }<br \/>\n        return left;<br \/>\n    }<br \/>\n};<\/p>\n<h4>28\u3001[\u7b80\u5355] \u627e\u51fa\u5b57\u7b26\u4e32\u4e2d\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\u7684\u4e0b\u6807<\/h4>\n<p>\u53cc\u6307\u9488 \u5b57\u7b26\u4e32 \u5b57\u7b26\u4e32\u5339\u914d<\/p>\n<p>class Solution {<br \/>\npublic:<br \/>\n    int strStr(string haystack, string needle) {<br \/>\n        int n &#061; haystack.size();<br \/>\n        for (int i &#061; 0; i &lt; n; i&#043;&#043;) {<br \/>\n            if (haystack[i] &#061;&#061; needle[0]) {<br \/>\n                int start &#061; 0;<br \/>\n                while (i &#043; start &lt; n &amp;&amp; start &lt; needle.size()) {<br \/>\n                    if (haystack[i &#043; start] &#061;&#061; needle[start]) {<br \/>\n                        start&#043;&#043;;<br \/>\n                    } else {<br \/>\n                        break;<br \/>\n                    }<br \/>\n                }<br \/>\n                if (start &#061;&#061; needle.size()) {<br \/>\n                    return i;<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n        return -1;<br \/>\n    }<br \/>\n};<\/p>\n<p>class Solution {<br \/>\npublic:<br \/>\n    int strStr(string haystack, string needle) {<br \/>\n        int n &#061; haystack.size();<br \/>\n        int m &#061; needle.size();<br \/>\n        for (int i &#061; 0; i &lt;&#061; n &#8211; m; i&#043;&#043;) {<br \/>\n            int j &#061; i, k &#061; 0;<br \/>\n            while (k &lt; m &amp;&amp; haystack[j] &#061;&#061; needle[k]) {<br \/>\n                j&#043;&#043;;<br \/>\n                k&#043;&#043;;<br \/>\n            }<br \/>\n            if (k &#061;&#061; m) {<br \/>\n                return i;<br \/>\n            }<br \/>\n        }<br \/>\n        return -1;<br \/>\n    }<br \/>\n};<\/p>\n<p>KMP<\/p>\n<p>class Solution {<br \/>\npublic:<br \/>\n    int strStr(string haystack, string needle) {<br \/>\n        int n &#061; haystack.size(), m &#061; needle.size();<br \/>\n        if (m &#061;&#061; 0) {<br \/>\n            return 0;<br \/>\n        }<br \/>\n        \/\/ \u8bbe\u7f6e\u54e8\u5175<br \/>\n        haystack.insert(haystack.begin(), &#039; &#039;);<br \/>\n        needle.insert(needle.begin(), &#039; &#039;);<br \/>\n        vector&lt;int&gt; next(m &#043; 1);<br \/>\n        \/\/ \u9884\u5904\u7406next\u6570\u7ec4<br \/>\n        for (int i &#061; 2, j &#061; 0; i &lt;&#061; m; i&#043;&#043;) {<br \/>\n            while (j &amp;&amp; needle[i] !&#061; needle[j &#043; 1]) {<br \/>\n                j &#061; next[j];<br \/>\n            }<br \/>\n            if (needle[i] &#061;&#061; needle[j &#043; 1]) {<br \/>\n                j&#043;&#043;;<br \/>\n            }<br \/>\n            next[i] &#061; j;<br \/>\n        }<br \/>\n        \/\/ \u5339\u914d\u8fc7\u7a0b<br \/>\n        for (int i &#061; 1, j &#061; 0; i &lt;&#061; n; i&#043;&#043;) {<br \/>\n            while (j &amp;&amp; haystack[i] !&#061; needle[j &#043; 1]) {<br \/>\n                j &#061; next[j];<br \/>\n            }<br \/>\n            if (haystack[i] &#061;&#061; needle[j &#043; 1]) {<br \/>\n                j&#043;&#043;;<br \/>\n            }<br \/>\n            if (j &#061;&#061; m) {<br \/>\n                return i &#8211; m;<br \/>\n            }<br \/>\n        }<br \/>\n        return -1;<br \/>\n    }<br \/>\n};<\/p>\n<h4>29\u3001[\u4e2d\u7b49] \u4e24\u6570\u76f8\u9664<\/h4>\n<p>\u6570\u5b66 \u4e8c\u5206\u67e5\u627e<\/p>\n<p>\u4e8c\u5206\u67e5\u627e<\/p>\n<p>class Solution {<br \/>\npublic:<br \/>\n    int divide(int dividend, int divisor) {<br \/>\n        \/\/ \u8003\u8651\u88ab\u9664\u6570\u4e3a\u6700\u5c0f\u503c\u7684\u60c5\u51b5<br \/>\n        if (dividend &#061;&#061; INT_MIN) {<br \/>\n            if (divisor &#061;&#061; 1) {<br \/>\n                return INT_MIN;<br \/>\n            }<br \/>\n            if (divisor &#061;&#061; -1) {<br \/>\n                return INT_MAX;<br \/>\n            }<br \/>\n        }<br \/>\n        \/\/ \u8003\u8651\u9664\u6570\u4e3a\u6700\u5c0f\u503c\u7684\u60c5\u51b5<br \/>\n        if (divisor &#061;&#061; INT_MIN) {<br \/>\n            return dividend &#061;&#061; INT_MIN ? 1 : 0;<br \/>\n        }<br \/>\n        \/\/ \u8003\u8651\u88ab\u9664\u6570\u4e3a 0 \u7684\u60c5\u51b5<br \/>\n        if (dividend &#061;&#061; 0) {<br \/>\n            return 0;<br \/>\n        }<\/p>\n<p>        \/\/ \u4e00\u822c\u60c5\u51b5&#xff0c;\u4f7f\u7528\u4e8c\u5206\u67e5\u627e<br \/>\n        \/\/ \u5c06\u6240\u6709\u7684\u6b63\u6570\u53d6\u76f8\u53cd\u6570&#xff0c;\u8fd9\u6837\u5c31\u53ea\u9700\u8981\u8003\u8651\u4e00\u79cd\u60c5\u51b5<br \/>\n        bool rev &#061; false;<br \/>\n        if (dividend &gt; 0) {<br \/>\n            dividend &#061; -dividend;<br \/>\n            rev &#061; !rev;<br \/>\n        }<br \/>\n        if (divisor &gt; 0) {<br \/>\n            divisor &#061; -divisor;<br \/>\n            rev &#061; !rev;<br \/>\n        }<\/p>\n<p>        \/\/ \u5feb\u901f\u4e58<br \/>\n        auto quickAdd &#061; [](int y, int z, int x) {<br \/>\n            \/\/ x \u548c y \u662f\u8d1f\u6570&#xff0c;z \u662f\u6b63\u6570<br \/>\n            \/\/ \u9700\u8981\u5224\u65ad z * y &gt;&#061; x \u662f\u5426\u6210\u7acb<br \/>\n            int result &#061; 0, add &#061; y;<br \/>\n            while (z) {<br \/>\n                if (z &amp; 1) {<br \/>\n                    \/\/ \u9700\u8981\u4fdd\u8bc1 result &#043; add &gt;&#061; x<br \/>\n                    if (result &lt; x &#8211; add) {<br \/>\n                        return false;<br \/>\n                    }<br \/>\n                    result &#043;&#061; add;<br \/>\n                }<br \/>\n                if (z !&#061; 1) {<br \/>\n                    \/\/ \u9700\u8981\u4fdd\u8bc1 add &#043; add &gt;&#061; x<br \/>\n                    if (add &lt; x &#8211; add) {<br \/>\n                        return false;<br \/>\n                    }<br \/>\n                    add &#043;&#061; add;<br \/>\n                }<br \/>\n                \/\/ \u4e0d\u80fd\u4f7f\u7528\u9664\u6cd5<br \/>\n                z &gt;&gt;&#061; 1;<br \/>\n            }<br \/>\n            return true;<br \/>\n        };<\/p>\n<p>        int left &#061; 1, right &#061; INT_MAX, ret &#061; 0;<br \/>\n        while (left &lt;&#061; right) {<br \/>\n            \/\/ \u6ce8\u610f\u6ea2\u51fa&#xff0c;\u5e76\u4e14\u4e0d\u80fd\u4f7f\u7528\u9664\u6cd5<br \/>\n            int mid &#061; left &#043; ((right &#8211; left) &gt;&gt; 1);<br \/>\n            bool check &#061; quickAdd(divisor, mid, dividend);<br \/>\n            if (check) {<br \/>\n                ret &#061; mid;<br \/>\n                \/\/ \u6ce8\u610f\u6ea2\u51fa<br \/>\n                if (mid &#061;&#061; INT_MAX) {<br \/>\n                    break;<br \/>\n                }<br \/>\n                left &#061; mid &#043; 1;<br \/>\n            } else {<br \/>\n                right &#061; mid &#8211; 1;<br \/>\n            }<br \/>\n        }<\/p>\n<p>        return rev ? -ret : ret;<br \/>\n    }<br \/>\n};<\/p>\n<p>\u7c7b\u4e8c\u5206\u67e5\u627e<\/p>\n<p>class Solution {<br \/>\npublic:<br \/>\n    int divide(int dividend, int divisor) {<br \/>\n        \/\/ \u8003\u8651\u88ab\u9664\u6570\u4e3a\u6700\u5c0f\u503c\u7684\u60c5\u51b5<br \/>\n        if (dividend &#061;&#061; INT_MIN) {<br \/>\n            if (divisor &#061;&#061; 1) {<br \/>\n                return INT_MIN;<br \/>\n            }<br \/>\n            if (divisor &#061;&#061; -1) {<br \/>\n                return INT_MAX;<br \/>\n            }<br \/>\n        }<br \/>\n        \/\/ \u8003\u8651\u9664\u6570\u4e3a\u6700\u5c0f\u503c\u7684\u60c5\u51b5<br \/>\n        if (divisor &#061;&#061; INT_MIN) {<br \/>\n            return dividend &#061;&#061; INT_MIN ? 1 : 0;<br \/>\n        }<br \/>\n        \/\/ \u8003\u8651\u88ab\u9664\u6570\u4e3a 0 \u7684\u60c5\u51b5<br \/>\n        if (dividend &#061;&#061; 0) {<br \/>\n            return 0;<br \/>\n        }<\/p>\n<p>        \/\/ \u4e00\u822c\u60c5\u51b5&#xff0c;\u4f7f\u7528\u7c7b\u4e8c\u5206\u67e5\u627e<br \/>\n        \/\/ \u5c06\u6240\u6709\u7684\u6b63\u6570\u53d6\u76f8\u53cd\u6570&#xff0c;\u8fd9\u6837\u5c31\u53ea\u9700\u8981\u8003\u8651\u4e00\u79cd\u60c5\u51b5<br \/>\n        bool rev &#061; false;<br \/>\n        if (dividend &gt; 0) {<br \/>\n            dividend &#061; -dividend;<br \/>\n            rev &#061; !rev;<br \/>\n        }<br \/>\n        if (divisor &gt; 0) {<br \/>\n            divisor &#061; -divisor;<br \/>\n            rev &#061; !rev;<br \/>\n        }<\/p>\n<p>        vector&lt;int&gt; candidates &#061; {divisor};<br \/>\n        \/\/ \u6ce8\u610f\u6ea2\u51fa<br \/>\n        while (candidates.back() &gt;&#061; dividend &#8211; candidates.back()) {<br \/>\n            candidates.push_back(candidates.back() &#043; candidates.back());<br \/>\n        }<br \/>\n        int ans &#061; 0;<br \/>\n        for (int i &#061; candidates.size() &#8211; 1; i &gt;&#061; 0; &#8211;i) {<br \/>\n            if (candidates[i] &gt;&#061; dividend) {<br \/>\n                ans &#043;&#061; (1 &lt;&lt; i);<br \/>\n                dividend -&#061; candidates[i];<br \/>\n            }<br \/>\n        }<\/p>\n<p>        return rev ? -ans : ans;<br \/>\n    }<br \/>\n};<\/p>\n<h4>30\u3001[\u56f0\u96be] \u4e32\u8054\u6240\u6709\u5355\u8bcd\u7684\u5b57\u4e32<\/h4>\n<p>\u54c8\u5e0c\u8868 \u5b57\u7b26\u4e32 \u6ed1\u52a8\u7a97\u53e3<\/p>\n<p>class Solution {<br \/>\npublic:<br \/>\n    vector&lt;int&gt; findSubstring(string s, vector&lt;string&gt;&amp; words) {<br \/>\n        unordered_map&lt;string, int&gt; hash1; \/\/ \u4fdd\u5b58 words \u91cc\u9762\u6240\u6709\u5355\u8bcd\u7684\u9891\u6b21<br \/>\n        for (const auto&amp; s : words) {<br \/>\n            hash1[s]&#043;&#043;;<br \/>\n        }<br \/>\n        vector&lt;int&gt; ret;<\/p>\n<p>        int len &#061; words[0].size(), m &#061; words.size();<br \/>\n        for (int i &#061; 0; i &lt; len; i&#043;&#043;) {<br \/>\n            unordered_map&lt;string, int&gt; hash2; \/\/ \u7ef4\u62a4\u7a97\u53e3\u5185\u5355\u8bcd\u7684\u9891\u6b21<br \/>\n            for (int left &#061; i, right &#061; i, count &#061; 0; right &#043; len &lt;&#061; s.size();<br \/>\n                 right &#043;&#061; len) {<br \/>\n                \/\/ \u8fdb\u7a97\u53e3 &#043; \u7ef4\u62a4 count<br \/>\n                string in &#061; s.substr(right, len);<br \/>\n                hash2[in]&#043;&#043;;<br \/>\n                if (hash1.count(in) &amp;&amp; hash2[in] &lt;&#061; hash1[in]) {<br \/>\n                    count&#043;&#043;;<br \/>\n                }<\/p>\n<p>                \/\/ \u5224\u65ad<br \/>\n                if (right &#8211; left &#043; 1 &gt; len * m) {<br \/>\n                    \/\/ \u51fa\u7a97\u53e3 &#043; \u7ef4\u62a4 count<br \/>\n                    string out &#061; s.substr(left, len);<br \/>\n                    if (hash1.count(out) &amp;&amp; hash2[out] &lt;&#061; hash1[out]) {<br \/>\n                        count&#8211;;<br \/>\n                    }<br \/>\n                    hash2[out]&#8211;;<br \/>\n                    left &#043;&#061; len;<br \/>\n                }<\/p>\n<p>                \/\/ \u66f4\u65b0\u7ed3\u679c<br \/>\n                if (count &#061;&#061; m) {<br \/>\n                    ret.push_back(left);<br \/>\n                }<br \/>\n            }<br \/>\n        }<\/p>\n<p>        return ret;<br \/>\n    }<br \/>\n};<\/p>\n","protected":false},"excerpt":{"rendered":"<p>21\u3001[\u7b80\u5355] \u5408\u5e76\u4e24\u4e2a\u6709\u5e8f\u94fe\u8868<br \/>\n\u9012\u5f52 \u94fe\u8868 \u8fed\u4ee3 class Solution {<br \/>\npublic:ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {ListNode head;ListNode *cur  &amp;head, *cur1  list1, *cur2  list2;while (cur1 &amp;&amp; cur2) {if (cur1-&gt;val val) {cur-&gt;next  cur1;c<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[8185,55,99,2692,81,1983,427],"topic":[],"class_list":["post-79377","post","type-post","status-publish","format-standard","hentry","category-server","tag-lenyiin","tag-c","tag-java","tag-leetcode","tag-python","tag-1983","tag-427"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u300aLeetCode \u987a\u5e8f\u5237\u9898\u300b21 - 30 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/79377.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u300aLeetCode \u987a\u5e8f\u5237\u9898\u300b21 - 30 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"21\u3001[\u7b80\u5355] \u5408\u5e76\u4e24\u4e2a\u6709\u5e8f\u94fe\u8868 \u9012\u5f52 \u94fe\u8868 \u8fed\u4ee3 class Solution { public:ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {ListNode head;ListNode *cur &amp;head, *cur1 list1, *cur2 list2;while (cur1 &amp;&amp; cur2) {if (cur1-&gt;val val) {cur-&gt;next cur1;c\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/79377.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-03-01T14:49:08+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/79377.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/79377.html\",\"name\":\"\u300aLeetCode \u987a\u5e8f\u5237\u9898\u300b21 - 30 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-03-01T14:49:08+00:00\",\"dateModified\":\"2026-03-01T14:49:08+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/79377.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/79377.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/79377.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u300aLeetCode \u987a\u5e8f\u5237\u9898\u300b21 - 30\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u300aLeetCode \u987a\u5e8f\u5237\u9898\u300b21 - 30 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/79377.html","og_locale":"zh_CN","og_type":"article","og_title":"\u300aLeetCode \u987a\u5e8f\u5237\u9898\u300b21 - 30 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"21\u3001[\u7b80\u5355] \u5408\u5e76\u4e24\u4e2a\u6709\u5e8f\u94fe\u8868 \u9012\u5f52 \u94fe\u8868 \u8fed\u4ee3 class Solution { public:ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {ListNode head;ListNode *cur &amp;head, *cur1 list1, *cur2 list2;while (cur1 &amp;&amp; cur2) {if (cur1-&gt;val val) {cur-&gt;next cur1;c","og_url":"https:\/\/www.wsisp.com\/helps\/79377.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-03-01T14:49:08+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"8 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/79377.html","url":"https:\/\/www.wsisp.com\/helps\/79377.html","name":"\u300aLeetCode \u987a\u5e8f\u5237\u9898\u300b21 - 30 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-03-01T14:49:08+00:00","dateModified":"2026-03-01T14:49:08+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/79377.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/79377.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/79377.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u300aLeetCode \u987a\u5e8f\u5237\u9898\u300b21 - 30"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/79377","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=79377"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/79377\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=79377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=79377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=79377"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=79377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}