LeetCode OJ: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 → 1[1,3,5,6], 7 → 4[1,3,5,6], 0 → 0
首先是自己一个比较蠢的方法,可能当时没怎么细细的想,大体的思路就是,将vector中元素存放到set中(因为set插入的时候已经排好序了),首先查找,找不到的话在插入,并且记下插入位置,指针递增到那个地方的时候就找到了那个位置。如果第一次找到那个位置的就直接递增找到那个位置即可,代码见下,很不优雅:
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
set<int>tmpSet(nums.begin(), nums.end());//因为set已经排好序了,所以用set
int i = ;
set<int>::iterator sItor;
if((sItor = (tmpSet.find(target))) == tmpSet.end())//不在set中的话,就先插入
sItor = tmpSet.insert(target);
for(auto itor = tmpSet.begin(); itor != it.first; ++itor){
i++;
return i;
}
};
但是其实在网上找了点好的答案,实际上这个就是二分法的一个小小的变形而已:
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int beg = ;
int end = nums.size() - ;
int mid;
while(beg <= end){
mid = (beg + end) >> ;
if(nums[mid] > target)
end = mid - ;
else if(nums[mid] < target)
beg = mid + ;
else
return mid;
}
int sz = nums.size();
if(end < ) reutrn ; //这个地方应该注意,不要搞反了
if(beg >= sz) reutrn sz;
return beg; //这一步应该注意,很关键
}
};
二分法不可小觑,细节还是很多的,仔细看看都能有不小的收获。其实实际上感觉写出来不太容易错的还是上面写的那种方法,不过那个总杆觉写出来怪怪的,不合题目的初衷了都。
java代码如下所示:
public class Solution {
public int searchInsert(int[] nums, int target) {
int beg = 0;
int end = nums.length - 1;
while(beg <= end){
int mid = beg + (end - beg)/2;
if(nums[mid] > target)
end--;
else if(nums[mid] < target)
beg++;
else
return mid;
}
if(beg >= nums.length)
return nums.length;
if(end < 0)
return 0;
return beg;
}
}
LeetCode OJ:Search Insert Position(查找插入位置)的更多相关文章
- [LeetCode] 35. Search Insert Position 搜索插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [leetcode]35. Search Insert Position寻找插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- Search insert position, 查找插入位置
问题描述:给定一个有序序列,如果找到target,返回下标,如果找不到,返回插入位置. 算法分析:依旧利用二分查找算法. public int searchInsert(int[] nums, int ...
- 【LeetCode】Search Insert Position(搜索插入位置)
这道题是LeetCode里的第35道题. 题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元 ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- [LeetCode] 035. Search Insert Position (Medium) (C++)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- LeetCode 035 Search Insert Position
题目要求:Search Insert Position Given a sorted array and a target value, return the index if the target ...
- [LeetCode] Search Insert Position 搜索插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- LeetCode 35 Search Insert Position(查找插入位置)
题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description 在给定的有序数组中插入一个目标数字,求出插入 ...
- Leetcode 35 Search Insert Position 二分查找(二分下标)
基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...
随机推荐
- 你真的会用Retrofit2吗?Retrofit2完全教程
本文注目录: Retrofit入门 Retrofit注解详解 Gson与Converter RxJava与CallAdapter 自定义Converter 自定义CallAdapter 其它说明 前言 ...
- ionic学习笔记—常用命令
Ionic CLI介绍 Ionic CLI是开发Ionic应用程序过程中使用的主要工具.它就像一个瑞士军刀:它在一个界面下汇集了大量工具. CLI包含许多对Ionic开发至关重要的命令,例如start ...
- php imagemagick库安装使用
imagemagick介绍: ImageMagick® is a software suite to create, edit, compose, or convert bitmap images. ...
- Python之字符编码(Day10)
1. python解释器执行py文件的原理 ,例如python test.py 第一阶段:python解释器启动,此时就相当于启动了一个文本编辑器 第二阶段:python解释器相当于文本编辑器, ...
- 微信小程序学习笔记(5)--------框架之视图层
这一系列转载:http://blog.csdn.net/zsp45212/article/details/53518238 视图层 框架的视图层由wxml与wxss编写,由组件进行展示.将逻辑层的数据 ...
- Linux网络性能评估工具iperf 、CHARIOT测试网络吞吐量
网络性能评估主要是监测网络带宽的使用率,将网络带宽利用最大化是保证网络性能的基础,但是由于网络设计不合理.网络存在安全漏洞等原因,都会导致网络带宽利用率不高.要找到网络带宽利用率不高的原因,就需要对网 ...
- LVS持久化
在实际应用场景中,轮询调度并不都是适用的.有些情况下,需要我们把同一个会话的请求都调度给一个RS节点.这时候就需要LVS提供持久化的能力,能够实现会话保持. 一.LVS的持久化主要包括以下两个方面. ...
- Book Review of “The practice of programming” (Ⅱ)
The practice of programming Chapter 2 Algorithms and Data Structures Searching sequential search (li ...
- 关于Block汇总
//使用总结: //1.当block里面会有b类相关的参数要回调回去的时候,属性用copy修饰,将其拷贝到堆里面,这样即便栈释放掉了,b类的指针也在堆中存在,能够成功的回调回去. //Block默认存 ...
- POJ - 2785 - 4 Values whose Sum is 0 - 二分折半查找
2017-08-01 21:29:14 writer:pprp 参考:http://blog.csdn.net/piaocoder/article/details/45584763 算法分析:直接暴力 ...