{"id":153,"date":"2017-09-23T22:46:31","date_gmt":"2017-09-23T14:46:31","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=153"},"modified":"2018-11-27T16:47:14","modified_gmt":"2018-11-27T08:47:14","slug":"167-two-sum-ii-input-array-is-sorted","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=153","title":{"rendered":"167. Two Sum II &#8211; Input array is sorted"},"content":{"rendered":"<p>Given an array of integers that is already\u00a0<strong><em>sorted in ascending order<\/em><\/strong>, 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.<\/p>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li>Your returned answers (both index1 and index2) are not zero-based.<\/li>\n<li>You may assume that each input would have\u00a0<em>exactly<\/em>\u00a0one solution and you may not use the\u00a0<em>same<\/em>\u00a0element twice.<\/li>\n<\/ul>\n<p><strong>Example:<\/strong><\/p>\n<pre><strong>Input:<\/strong> numbers = [2,7,11,15], target = 9\r\n<strong>Output:<\/strong> [1,2]\r\n<strong>Explanation:<\/strong> The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.<\/pre>\n<p><strong>Python<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\nclass Solution(object):\r\n    def twoSum(self, numbers, target):\r\n        &quot;&quot;&quot;\r\n        :type numbers: List[int]\r\n        :type target: int\r\n        :rtype: List[int]\r\n        &quot;&quot;&quot;\r\n        left = 0\r\n        right = len(numbers) - 1\r\n        while left &lt; right:\r\n            temp_sum = numbers[left] + numbers[right]\r\n            if temp_sum &gt; target:\r\n                right -= 1\r\n            elif temp_sum &lt; target:\r\n                left += 1\r\n            else:\r\n                return [left+1, right+1]\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of integers that is already\u00a0sorted in as<a href=\"https:\/\/www.ccagml.com\/?p=153\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">167. Two Sum II &#8211; Input array is sorted<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/153"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=153"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/153\/revisions"}],"predecessor-version":[{"id":154,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/153\/revisions\/154"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}