Search Insert Position [LeetCode]
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
Summary: basic binary search stuff.
int searchInsert(int A[], int n, int target) {
int start = ;
int end = n - ;
while(start <= end) {
int median = start + (end - start + ) / ;
if(target == A[median]) {
return median;
}else if(target < A[median]) {
if(median - >= && target > A[median - ])
return median;
if(median == )
return ;
end = median - ;
}else {
if(median + <= n - && target < A[median + ])
return median + ;
if(median == n - )
return n;
start = median + ;
}
}
}
Search Insert Position [LeetCode]的更多相关文章
- Search Insert Position——LeetCode
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- Search Insert Position leetcode java
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
- [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之二分法专题-35. 搜索插入位置(Search Insert Position)
Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position) 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会 ...
- LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version
704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...
- LeetCode: Search Insert Position 解题报告
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
随机推荐
- mvc 数据验证金钱格式decimal格式验证
mvc 数据验证金钱格式decimal格式验证 首先看下代码 /// <summary> /// 产品单价 /// </summary> [Display(Name = &qu ...
- 对字符串算md5
这个问题要是写代码 是很简单的一个问题 能不能再简单一点呢,比如一条命令 一条sql,当然你要是在线转换也很快 shell printf admin|md5sum 注意printf 与echo区别 ...
- 【转】SQLServerDBA十大必备工具---让生活轻松点
曾经和一些DBA和数据库开发人员交流时,问他们都用过一些什么样的DB方面的工具,大部分人除了SSMS和Profile之外,基本就没有使用过其他工具了: 诚然,SSMS和Profile足够强大,工作的大 ...
- 计划将项目中使用entity framework的要点记录到改栏目下
ef监控sql执行性能日志.http://www.cnblogs.com/CreateMyself/p/5277681.html http://123.122.205.38/cn_sql_server ...
- Number类型方法
//1.toString(); 转换成字符串 var s=123; console.log(typeof s.toString()); //string //2.toLocaleString() ...
- 【selenium 3】 Mac 下测试环境搭建 Firefox 47+ gecko driver Mac
错误代码如下:File "/usr/local/lib/python2.7/dist-packages/selenium-3.0.0b2-py2.7.egg/selenium/webdriv ...
- [原创]java WEB学习笔记95:Hibernate 目录
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [技术分享] .NET下 , 上传图片的处理方式 , 贴上代码 .
如题 , 直接上单代码 , AC代码 <!DOCTYPE HTML> <html> <head> <meta charset="utf-8" ...
- 【01:转自知乎:关于 openSUSE 】
我是 openSUSE 中文维基唯一的非官方维护者,openSUSE 简体中文翻译团队召集人,linuxsir SuSE 版块的版主,openSUSE 官方论坛 http://forums.opens ...
- MS CRM商机产品等Mapping
商机明细:报价明细 SELECT EntityMapId FROM EntityMapBase WHERE TargetEntityName='quotedetail' AND So ...