{"id":6213,"date":"2020-01-31T22:01:52","date_gmt":"2020-02-01T06:01:52","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6213"},"modified":"2020-01-31T22:02:17","modified_gmt":"2020-02-01T06:02:17","slug":"leetcode-35-search-insert-position","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-35-search-insert-position\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 35. Search Insert Position"},"content":{"rendered":"\n<p>Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.<\/p>\n\n\n\n<p>You may assume no duplicates in the array.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> [1,3,5,6], 5\n<strong>Output:<\/strong> 2\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> [1,3,5,6], 2\n<strong>Output:<\/strong> 1\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> [1,3,5,6], 7\n<strong>Output:<\/strong> 4\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> [1,3,5,6], 0\n<strong>Output:<\/strong> 0<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Binary Search<\/strong><\/h2>\n\n\n\n<p>Find the number or upper bound if doesn&#8217;t exist.<\/p>\n\n\n\n<p>Time complexity: O(logn)<br>Space complexity: O1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int searchInsert(vector<int>& nums, int target) {\n    int l = 0, r = nums.size();\n    while (l < r) {\n      int m = l + (r - l) \/ 2;      \n      if (nums[m] == target)\n        return m;\n      else if (nums[m] > target)\n        r = m;\n      else\n        l = m + 1;\n    }\n    return l;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149],"tags":[52,222,376],"class_list":["post-6213","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-binary-search","tag-easy","tag-on","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6213","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=6213"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6213\/revisions"}],"predecessor-version":[{"id":6215,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6213\/revisions\/6215"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}