Leetcode练习题Search Insert Position
Question:
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.
Example 1:
Input: [1,3,5,6], 5
Output: 2
Example 2:
Input: [1,3,5,6], 2
Output: 1
Example 3:
Input: [1,3,5,6], 7
Output: 4
Example 4:
Input: [1,3,5,6], 0
Output: 0
Answer
class Solution {
public int searchInsert(int[] nums, int target) {
int left = 0;
int right = nums.length-1;
while(left<right)
{
int mid = (left + right)/2;
if(nums[mid]>target)
{
right = mid - 1;
}
else if(nums[mid]<target)
{
left = mid + 1;
}
else
{
return mid;
}
}
if(nums[left]<target)
{
return left+1;
}
else
{
return left;
}
}
}
效率:

Leetcode练习题Search Insert Position的更多相关文章
- [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] 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
1 题目: Given a sorted array and a target value, return the index if the target is found. If not, retu ...
- Leetcode 35 Search Insert Position 二分查找(二分下标)
基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数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 解决思路
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 ...
随机推荐
- Linux系统学习 二十二、SAMBA服务—Samba基本使用—share权限访问、客户端的使用
share权限访问 配置文件修改 [global]全局设置 workgroup=MYGROUP server string=Samba Server Lamp log file=/var/log/sa ...
- Samba共享文件
1 安装samba yum install -y samba* 2 添加用户 useradd smbuser 3 设置共享文件用户的密码 smbpasswd -a smbuser 4 创建公共共享文件 ...
- 《数据挖掘导论》实验课——实验四、数据挖掘之KNN,Naive Bayes
实验四.数据挖掘之KNN,Naive Bayes 一.实验目的 1. 掌握KNN的原理 2. 掌握Naive Bayes的原理 3. 学会利用KNN与Navie Bayes解决分类问题 二.实验工具 ...
- 「Shimo使用指南」mac支持pptp协议的小软件
Mac的好多小伙伴在访问网络设备时觉得远程连接不方便,例如ssh,***登陆都不是很方便,后来又安装了open*** forMac.ISSH等客户端,使用后发现不是很稳定,断线后很久都无法连接等缺点, ...
- 关于Mysql修改帐号密码的多种方法
方法一: 登录Mysql进行修改(mysql的小黑框) 命令: set password for 用户名(一般为root) @localhost=password('新密码'); 方法二: 登录MyS ...
- 「专题总结」LCT 2
差不多理解板子之后,写了一些奇怪的题. 但是还是那个问题:树剖真好使. 魔法森林:mikufun说这个是傻逼题. 为了得到书法大家的真传,小 E 同学下定决心去拜访住在魔法森林中的隐士. 魔法森林可以 ...
- Laravel-权限系统
总结Auth中间件用于定义未登录用户只能操作哪些权限policy授权策略定义了当前用户实例与进行授权的用户是否匹配,一致才能进一步操作,否则返回403禁止访问异常场景:用户登录 Auth步骤 找到需要 ...
- fjnu2019第二次友谊赛 B题
### 题目链接 ### 题目大意: 给你一个 n * m 的地图以及小蛇蛇头的初始位置,告诉你它会往 上.下.左.右 四个方向走.若在走的过程中(包括结束时)会使得小蛇越界,则输出 "Ga ...
- Implement Custom Business Classes and Reference Properties实现自定义业务类和引用属性(EF)
In this lesson, you will learn how to implement business classes from scratch. For this purpose, the ...
- Android MediaPlayer 在 6.0 以上版本使用倍速播放功能说明
Android MediaPlayer 在API 23即6.0版本开始支持倍速播放,下面我们来介绍一下如何使用MediaPlayer进行倍速播放. 一.核心接口 MediaPlayer.setPlay ...