035 Search Insert Position 搜索插入位置
给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引。如果没有,返回到它将会被按顺序插入的位置。
你可以假设在数组中无重复元素。
案例 1:
输入: [1,3,5,6], 5
输出: 2
案例 2:
输入: [1,3,5,6], 2
输出: 1
案例 3:
输入: [1,3,5,6], 7
输出: 4
案例 4:
输入: [1,3,5,6], 0
输出: 0
详见:https://leetcode.com/problems/search-insert-position/description/
Java实现:
class Solution {
public int searchInsert(int[] nums, int target) {
int size=nums.length;
if(size==0||nums==null){
return -1;
}
int l=0;
int r=size-1;
int m=0;
while(l<=r){
m=(l+r)>>1;
if(nums[m]==target){
return m;
}else if(nums[m]>target){
--r;
}else{
++l;
}
}
return l;
}
}
C++实现:
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int size=nums.size();
if(size==0||nums.empty())
{
return -1;
}
int l=0,r=size-1,m=0;
while(l<=r)
{
m=(l+r)>>1;
if(nums[m]==target)
{
return m;
}
else if(nums[m]>target)
{
r=m-1;
}
else
{
l=m+1;
}
}
return l;
}
};
035 Search Insert Position 搜索插入位置的更多相关文章
- lintcode:Search Insert Position 搜索插入位置
题目: 搜索插入位置 给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引.如果没有,返回到它将会被按顺序插入的位置. 你可以假设在数组中无重复元素. 样例 [1,3,5,6],5 → 2 ...
- [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 搜索插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【LeetCode】Search Insert Position(搜索插入位置)
这道题是LeetCode里的第35道题. 题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元 ...
- [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]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每天一题】Search Insert Position(搜索查找位置)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [Leetcode] search insert position 寻找插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
随机推荐
- 命令行启动nodejs方式 小总结
之前启动nodejs都是写一个命令行文件,如nodejs.cmd,内容为:start node E:\node\app.js. 今天突然想到之前也用过另外一种方式启动,就是在命令行通过cd命令先找到n ...
- 51nod 1450 闯关游戏——期望dp
题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1450 想了半天,不知道不能走的状态(即最后不足m个的状态)怎么办. ...
- C#某月的第一天和最后一天
1.本月的第一天===>DateTime.Now.AddDays(1 - DateTime.Now.Day);//当前日期减去当前日期和本月一号相差天数 2.本月的最后一天===>Date ...
- uboot和内核分区的修改
随着内核的更新,内核越来越大,uboot给nand的kernel分区默认是2M的 device nand0 <nandflash0>, # parts = 4 #: name ...
- java基础知识(7)---多态
多 态:(面向对象特征之一):函数本身就具备多态性,某一种事物有不同的具体的体现.体现:父类引用或者接口的引用指向了自己的子类对象.//Animal a = new Cat();多态的好处:提高了程序 ...
- VisualGDB系列1:VisualGDB总体概述
根据VisualGDB官网(https://visualgdb.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指正. 本文总体介绍VisualGDB能给你带来 ...
- linux日常管理-rsync_ssh方式
现在我们有两台机器,两台机器都需要安装rsync yum -y install rsync 一台的主机名是wangshaojun IP是192.168.1.117 ,另一台的主机名是 ...
- Linux TCP通信例子
TCP通信的C/S模型 ///server.c #include <sys/types.h> #include <sys/socket.h> #include <stdi ...
- 17_点击事件第四种写法_布局文件添加onclick属性
尽量不要用第四种点击事件的写法.在一万多行代码中发现了一个没被调用的代码 public void call(View v){//第四种写法参数一定是View v //public void call( ...
- JS设置cookie、读取cookie、删除cookie(转)
JS设置cookie.读取cookie.删除cookie 转载 2015-04-17 投稿:hebedich 我要评论 Js操作Cookie总结(设置,读取,删除),工作中经常会用到的哦! ...