{"id":903,"date":"2021-04-28T22:27:52","date_gmt":"2021-04-29T05:27:52","guid":{"rendered":"http:\/\/cywang.no-ip.org\/wordpress\/?p=903"},"modified":"2021-05-10T22:38:09","modified_gmt":"2021-05-11T05:38:09","slug":"167-two-sum-ii-input-array-is-sorted","status":"publish","type":"post","link":"http:\/\/cywang.no-ip.org\/wordpress\/?p=903","title":{"rendered":"167. Two Sum II &#8211; Input array is sorted"},"content":{"rendered":"<p><a href=\"https:\/\/leetcode.com\/problems\/two-sum-ii-input-array-is-sorted\/\" target=\"_blank\" rel=\"noopener\">167. Two Sum II &#8211; Input array is sorted<\/a><\/p>\n<p><!--more--><\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\"># Approach 1. Two Pointers\r\n# Time Complexity: O(n)\r\n# Space Complexity: O(1)\r\nclass Solution:\r\n    def twoSum(self, numbers: List[int], target: int) -&gt; List[int]:\r\n        i = 0\r\n        j = len(numbers) - 1\r\n        # because we assume it has exactly one solution, we don't need to consider i &gt; j case\r\n        while numbers[i] + numbers[j] != target:\r\n            if numbers[i] + numbers[j] &gt; target:\r\n                # the sum is too large, move j\r\n                j -= 1\r\n            else:\r\n                # the sum is too small, move i\r\n                i += 1\r\n        # because the indices are 1-indexed, return i+1 and j+1\r\n        return [i + 1, j + 1]<\/code><\/pre>\n<p>&nbsp;<\/p>\n<hr \/>\n<h1>Notes:<\/h1>\n<p>\u7a0b\u5f0f\u6d41\u7a0b<\/p>\n<ul>\n<li>\u6839\u64da\u984c\u76ee\u8aaa\u660e\uff0c\u4e00\u5b9a\u6709\u552f\u4e00\u89e3\u5b58\u5728\uff0c\u6240\u4ee5\u5ffd\u7565\u4e00\u4e9bcorner cases\u7684\u5224\u65b7<\/li>\n<li>Pseudo-code\n<pre class=\"line-numbers\"><code class=\"language-python\">i = representing the first element (0)\r\nj = representing the last element (length - 1)\r\n\r\nwhile numbers[i] +  numbers[j] != target\r\n    if numbers[i] + numbers[j] is greater than target, we shift the right pointer to left ( j = j -1)\r\n    if numbers[i] + numbers[j] is smaller than target, we shift the left pointer to right ( i = i + 1)\r\n\r\nfound pair and return [i+1, j+1]<\/code><\/pre>\n<\/li>\n<\/ul>\n<p>\u7d00\u9304<\/p>\n<ul>\n<li>5\u5206\u9418 \uff1a \u5beb\u4e0b\u984c\u76ee\u5927\u7db1\u3001\u9700\u8981\u7684\u5047\u8a2d\u3001method\u7684input\u8207output<\/li>\n<li>5\u5206\u9418 \uff1a \u8003\u616ebinary search solution\uff0c\u4f46\u662ftime complexity\u662fO(nlogn)\uff0c\u6bd4\u8d77non-sorted array\u7248\u672c (hash)\u9084\u6162\uff0c\u6240\u4ee5\u4e26\u672a\u5be6\u4f5c<\/li>\n<li>10\u5206\u9418\u5beb\u4e0bpseudo code\uff0c\u4e26\u8f49\u6210Python code\uff0c\u8209\u5e7e\u500b\u4f8b\u5b50\u7528\u624b\u5beb(\u6a21\u64ec)\u7684\u65b9\u5f0f\u8dd1\u904e\u4e00\u6b21\u7a0b\u5f0f\u78bc<\/li>\n<li>5\u5206\u9418\uff1a\u628a\u624b\u5beb\u7684\u7a0b\u5f0f\u78bc\u8f38\u5165Leetcode\u4e26\u57f7\u884c<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>\u5099\u8a3b<\/p>\n<ul>\n<li>\u984c\u76ee\u7d66\u7684\u5047\u8a2d\u7c21\u5316\u4e86\u7a0b\u5f0f\u7684\u5224\u65b7\uff0c\u4f8b\u5982\u4e0d\u9700\u8981\u8003\u616e\u627e\u4e0d\u5230\u89e3\u7b54\u7684\u60c5\u6cc1\uff0c\u4f46\u662f\u5982\u679c\u9762\u8a66\u7684\u6642\u5019\u4e26\u6c92\u6709\u9019\u4e9b\u5047\u8a2d\uff0ccoding\u7684\u6642\u5019\u8981\u7279\u5225\u6ce8\u610f\u4e00\u4e9b corner cases<\/li>\n<li>\u6bd4\u8d77\u539f\u672c\u7684two sum\u7248\u672c\uff0c\u4e0d\u9700\u8981\u984d\u5916\u7684\u7a7a\u9593 (Space Complexity: O(1))<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>167. Two Sum II &#8211; Input array is sorted<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[39],"tags":[],"class_list":["post-903","post","type-post","status-publish","format-standard","hentry","category-leetcode"],"_links":{"self":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/903","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=903"}],"version-history":[{"count":3,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/903\/revisions"}],"predecessor-version":[{"id":918,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/903\/revisions\/918"}],"wp:attachment":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=903"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}