{"id":1532,"date":"2018-01-07T19:01:12","date_gmt":"2018-01-08T03:01:12","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1532"},"modified":"2020-03-09T17:57:15","modified_gmt":"2020-03-10T00:57:15","slug":"leetcode-167-two-sum-ii-input-array-is-sorted","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-167-two-sum-ii-input-array-is-sorted\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 167. Two Sum II &#8211; Input array is sorted"},"content":{"rendered":"<div class=\"question-description\">\n<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u6392\u8fc7\u5e8f\u7684\u6570\u7ec4\uff0c\u8ba9\u4f60\u8f93\u51fa\u4e24\u4e2a\u6570\u7684index\uff0c\u4ed6\u4eec\u7684\u548c\u7b49\u4e8etarget\u3002<\/p>\n<p>Given an array of integers that is already&nbsp;<b><i>sorted in ascending order<\/i><\/b>, find two numbers such that they add up to a specific target number.<\/p>\n<p>The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.<\/p>\n<p>You may assume that each input would have&nbsp;<i>exactly<\/i>&nbsp;one solution and you may not use the&nbsp;<i>same<\/i>&nbsp;element twice.<\/p>\n<p><b>Input:<\/b>&nbsp;numbers={2, 7, 11, 15}, target=9<br \/>\n<b>Output:<\/b>&nbsp;index1=1, index2=2<\/p>\n<\/div>\n<p><strong>Solution:<\/strong><\/p>\n<p>C++ \/ two pointers<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\n\/\/ Running time: 6 ms\nclass Solution {\npublic:\n    vector&lt;int&gt; twoSum(vector&lt;int&gt;&amp; numbers, int target) {\n        int i = 0;\n        int j = numbers.size() - 1;\n        while (i &lt; j) {\n            const int sum = numbers[i] + numbers[j];\n            if (sum == target)\n                break;\n            else if (sum &lt; target)\n                ++i;\n            else\n                --j;\n        }\n        return {i + 1, j + 1};\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>C++ \/ Binary search<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\n\/\/ Running time: 6 ms\nclass Solution {\npublic:\n    vector&lt;int&gt; twoSum(vector&lt;int&gt;&amp; numbers, int target) {\n        const int n = numbers.size();\n        for (int i = 0; i &lt; n; ++i) {\n            int l = i + 1;\n            int r = n;\n            int t = target - numbers[i];\n            while (l &lt; r) {\n                int m = l + (r - l) \/ 2;\n                if (numbers[m] == t)\n                    return {i + 1, m + 1};\n                else if (numbers[m] &lt; t)\n                    l = m + 1;\n                else\n                    r = m;\n            }\n        }\n        return {};\n    }\n};<\/pre>\n<p><strong>Related Problems:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1-two-sum\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 1. Two Sum<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-653-two-sum-iv-input-is-a-bst\/\">\u82b1\u82b1\u9171 LeetCode 653. Two Sum IV &#8211; Input is a BST<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u6392\u8fc7\u5e8f\u7684\u6570\u7ec4\uff0c\u8ba9\u4f60\u8f93\u51fa\u4e24\u4e2a\u6570\u7684index\uff0c\u4ed6\u4eec\u7684\u548c\u7b49\u4e8etarget\u3002 Given an array of integers that is already&nbsp;sorted in ascending order, find two numbers such that they add up to a specific target number.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184,149,176],"tags":[52,208],"class_list":["post-1532","post","type-post","status-publish","format-standard","hentry","category-array","category-binary-search","category-two-pointers","tag-binary-search","tag-two-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1532","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=1532"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1532\/revisions"}],"predecessor-version":[{"id":6441,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1532\/revisions\/6441"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}