LeetCode OJ:Search Insert Position(查找插入位置)

首线是自己一个比较蠢的方法,可能当时没怎么细细的想,大体的思路就是,将vector中元素存放到set中(因为set插入的时候已经排好序了),首先查找,找不到的话在插入,兵器记下插入位置,指针递增到那个地方的时候就找到了那个位置。如果第一次找到那个位置的就直接递增找到那个位置即可,代码见下,很不优雅:

 1 class Solution {
 2 public:
 3     int searchInsert(vector<int>& nums, int target) {
 4         set<int>tmpSet(nums.begin(), nums.end());//因为set已经排好序了,所以用set
 5         int i = 0;
 6         set<int>::iterator sItor;
 7         if((sItor = (tmpSet.find(target))) == tmpSet.end())//不在set中的话,就先插入
 8             sItor = tmpSet.insert(target);
 9         for(auto itor = tmpSet.begin(); itor != it.first; ++itor){
10             i++;
11         return i;
12     }
13 };

但是其实在网上找了点好的答案,实际上这个就是二分法的一个小小的变形而已:

 1 class Solution {
 2 public:
 3     int searchInsert(vector<int>& nums, int target) {
 4         int beg = 0;
 5         int end = nums.size() - 1;
 6         int mid;
 7         while(beg <= end){
 8             mid = (beg + end) >> 1;
 9             if(nums[mid] > target)
10                 end = mid - 1;
11             else if(nums[mid] < target)
12                 beg = mid + 1;
13             else
14                 return mid;
15         }
16         int sz = nums.size();
17         if(end < 0) reutrn 0;    //这个地方应该注意,不要搞反了
18         if(beg >= sz) reutrn sz;
19         return beg;    //这一步应该注意,很关键
21     }
22 };

二分法不可小觑,细节还是很多的,仔细看看都能有不要的收获。其实实际上感觉写出来不太容易错的还是上面写的那种方法。

时间: 2024-10-24 12:08:32

LeetCode OJ:Search Insert Position(查找插入位置)的相关文章

leetCode 35.Search Insert Position (搜索插入位置) 解题思路和方法

Search Insert Position 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. You may assume no duplicates in the array. Here are few examples. [1,3,5

Search insert position, 查找插入位置

问题描述:给定一个有序序列,如果找到target,返回下标,如果找不到,返回插入位置. 算法分析:依旧利用二分查找算法. 1 public int searchInsert(int[] nums, int target) 2 { 3 return binarySearch(nums, 0, nums.length - 1, target); 4 } 5 public int binarySearch(int[] nums, int left, int right, int target) 6 {

【LeetCode】- Search Insert Position(查找插入的位置)

[ 问题: ] 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. You may assume no duplicates in the array. 翻译:给你一个排好序的数组和一个目标值,请找出目标值可以插入数组的位置. [ 分析: ]

【LeetCode】Search Insert Position (2 solutions)

Search Insert Position 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. You may assume no duplicates in the array. Here are few examples.[1,3,5,

[LeetCode] 035. Search Insert Position (Medium) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Search Insert Position (Medium) 链接: 题目:https://leetcode.com/problems/search-insert-position/ 代码(github):https://github.com/illuz/leetcode 题意: 要把一个数有序插入到一

【leetcode】Search Insert Position

Search Insert Position 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. You may assume no duplicates in the array. Here are few examples.[1,3,5,

LeetCode 035 Search Insert Position

题目要求:Search Insert Position 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. You may assume no duplicates in the array. Here are few examples.[1

LeetCode 35 Search Insert Position(查找插入位置)

题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description 在给定的有序数组中插入一个目标数字,求出插入该数字的下标 由于该数组是已经排好序的数组,可以利用二分查找. 二分查找的返回结果: 1. 当查找的数字在数组中时,返回第一次出现的下标 2. 当查找的数字不存在时,返回 - pos - 1(即 应当插入位置的相反数再减去 参考代码: package leetcode_50; import java.u

Leetcode 35 Search Insert Position 二分查找(二分下标)

基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第一题的解法,比较类似,当然是今天临时写的. 知道了这题就完成了leetcode 4的第二重二分的写法了吧,楼主懒... 1 class Solution { 2 public: 3 int searchInsert(vector<int>& nums, int target) { 4 in

[Leetcode] search insert position 寻找插入位置

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. You may assume no duplicates in the array. Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 →