LeetCode Problem 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
思路1:直观的想法,遍历数组,一旦当前元素>=target,当前元素的位置即为插入位置,边界问题处理一下即可。时间复杂度:O(n)
class Solution {
public:
int searchInsert(int A[], int n, int target) { if(target > A[n-])
return n; else {
for(int i = ; i < n; i++) {
if(A[i] >= target)
return i;
}
}
}
};
思路2:由于给定数组为已经排好序的,可以使用二分查找,比较A[mid]与target的大小。时间复杂度:O(logn)
class Solution {
public:
int searchInsert(int A[], int n, int target) { int start = ;
int end = n - ;
int mid = ; while(end >= start) { mid = (start + end) / ;
if(target == A[mid])
return mid;
else if(target > A[mid])
start = mid + ;
else
end = mid - ;
} if(target > A[mid])
return mid + ;
else
return mid; }
};
LeetCode Problem 35:Search Insert Position的更多相关文章
- [Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
- 【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 ...
- 【一天一道LeetCode】#35. Search Insert Position
一天一道LeetCode系列 (一)题目 Given a sorted array and a target value, return the index if the target is foun ...
- LeetCode:35. Search Insert Position(Easy)
1. 原题链接 https://leetcode.com/problems/search-insert-position/description/ 2. 题目要求 给定一个已经排好序的数组和一个目标值 ...
- Leetcode No.35 Search Insert Position(c++实现)
1. 题目 1.1 英文题目 Given a sorted array of distinct integers and a target value, return the index if the ...
- 【LeetCode】35. Search Insert Position 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...
- LeetCode OJ 35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [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导致的溢 ...
随机推荐
- EffectiveJava(23)为什么不能在新生代码中使用原生态类型
泛型类和泛型接口 声明一个或者多个类型参数的类或者接口. 为什么不要在新代码中使用原生态类型 原生态类型,即泛型不带参数的类型 如List的list,list就是其原生态类型 1.使用原生态类型,插入 ...
- diamond淘宝框架使用
转载:http://blog.csdn.net/coolyqq/article/details/50435634 一.概况 diamond是淘宝内部使用的一个管理持久配置的系统,它的特点是简单.可靠. ...
- Selenium webdriver Java 查找元素
1.简单查找 By ID: WebElement element=driver.findElement(By.id("userId")); By Name:WebElement e ...
- mysql 5.6 修改root原始密码不为空方法
mysql 5.6安装好之后,是默认root用户的密码为空的,此时为了安全性需要修改密码不为空,修改方法为: cmd或者mysql 5.6 command line client登陆之后,输入一下命令 ...
- iOS tableView下拉图片放大
事实上这个效果,本质上就是在你tableView下拉 造成offset时候. 保持你顶部图片的y坐标点还停留在下拉时屏幕的顶点(offset), 而图片的长度变为原始的height长度-(offset ...
- null和undefined相等比较
在==(相等)判断中,null和undefined相等(它们也与自身相等),除此之外不与其他值相等. 示例代码: <!DOCTYPE html> <html lang="z ...
- android 内部类的优化
developer.android.com 文档中有一篇关于性能的文章,里面提到了内部类的使用.文章建议"对于私有内部类 使用 包訪问权限取代私有权限訪问", 这里说的是在内部类訪 ...
- MyTest——边界检测
实现思路如下: Step1:大文件的内存映射,多线程数据读取,加快读取速度. Step2:点云数据预处理(抽希.去噪点). Step3:Step2处理后数据使用kdtree进行离散点排序. Step4 ...
- Centos中配置环境变量
以Java的开发环境Jdk为例. 将jdk-9.0.1放置在/usr/local下(UNIX规范),然后我们将jdk配置到环境变量中去. $ mv jdk- /usr/local $ vim /etc ...
- Centos7 卸载rpm包、卸载yum安装的包
1. 通过rpm -q <关键字>查到rpm包的名字.2. 调用rpm -e <包名>删除特定的rpm包