35. 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
代码:
第一反应,遍历了一遍:
public int searchInsert(int[] nums, int target) {
int result =0;
for (int i = 0 ; i < nums.length ; i++)
{
if (target <= nums[i])
{
result = i;
break;
}
else
{
result=i+1;
}
}
return result;
}
网上查了一下,发现二分法,算法复杂度会减少不少,尤其在较大的数据量:
int searchInsert_mid(int[] nums, int target) {
int low = 0, high = nums.length-1;
while(low <= high) {
int mid = (low+high)>>1;
if(nums[mid] == target)
return mid;
else if(nums[mid] > target)
high = mid-1;
else
low = mid+1;
}
if(high < 0) return 0;
if(low >= nums.length) return nums.length;
return low;
}
35. Search Insert Position的更多相关文章
- [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 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练题——35. Search Insert Position
1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value ...
- 【LeetCode】35. Search Insert Position (2 solutions)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- 35. Search Insert Position@python
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
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 解决思路
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 ...
随机推荐
- Tomcat 服务器性能优化
简介 考虑一下这种场景,你开发了一个应用,它有十分优秀的布局设计,最新的特性以及其它的优秀特点.但是在性能这方面欠缺,不管这个应用如何都会遭到客户拒绝.客户总是期望它们的应用应该有更好的性能.如果你在 ...
- Github如何删除repository(仓库)
首先就是你的Github主页了. 第二步点击进入一个repository(仓库) 第三步点击右上的setting 将此页面滑动到最下面找个这个 点击删除即可!
- 2015安徽省赛 I.梯田
http://xcacm.hfut.edu.cn/problem.php?id=1213 set + 搜索 姐姐是用搜索+二分做的,效率要高很多 #include<iostream> #i ...
- ARPACK在window visual Studio的安装配置
ARPACK是一个求解大规模稠密/稀疏矩阵问题的库,最近在做特征值问题时用到.ARPACK这库相当古老,最早是RICE的一帮人弄的.LAPACK也差不多,貌似是美帝某个.gov发起的.这俩源代码是Fo ...
- Tomcat配置文件server.xml详解
<?xml version='1.0' encoding='utf-8'?> <Server port="8005" shutdown="SHUTDOW ...
- 转:SQL子句的执行顺序
SQL 不同于与其他编程语言的最明显特征是处理代码的顺序.在大数编程语言中,代码按编码顺序被处理,但是在SQL语言中,第一个被处理的子句是FROM子句,尽管SELECT语句第一个出现,但是几乎总是最后 ...
- UIView的一些基本方法 init、loadView、viewDidLoad、viewDidUnload、dealloc
init方法 在init方法中实例化必要的对象(遵从LazyLoad思想) init方法中初始化ViewController本身 loadView方法 当view需要被展示而它却是nil时,view ...
- Linux下VMware虚拟机网卡不能运行在混杂模式解决办法
转自: http://blog.csdn.net/henulwj/article/details/50347489 问题描述 在Linux如果以普通用户运行VMware Workstations,创建 ...
- POJ 1503
http://poj.org/problem?id=1503 对于这个题我也是醉了,因为最开始是有学长和我们说过这个题目的,我以为我记得题目是什么意思,也就没看题目,结果按案例去理解题意,结果WA了一 ...
- Twisted安装
Debian sudo apt-get install gcc python-dev && sudo pip install twisted CentOS sudo yum insta ...