【LintCode】060.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.
[1,3,5,6],5 → 2 [1,3,5,6],2 → 1 [1,3,5,6], 7 → 4 [1,3,5,6],0 → 0
题解:
Solution 1 ()
class Solution {
/**
* param A : an integer sorted array
* param target : an integer to be inserted
* return : an integer
*/
public:
int searchInsert(vector<int> &A, int target) {
if (A.size() == ) {
return ;
}
int start = ;
int end = A.size() - ; while (start + < end) {
int mid = start + (end - start) / ;
if (A[mid] == target) {
return mid;
} else if (A[mid] > target) {
end = mid;
} else {
start = mid;
}
}
if (A[start] >= target) {
return start;
} else if (A[end] >= target) {
return end;
} else {
return end + ;
}
}
};
Solution 2 ()
class Solution {
/**
* param A : an integer sorted array
* param target : an integer to be inserted
* return : an integer
*/
public:
int searchInsert(vector<int> &A, int target) {
if (A.size() == ) {
return ;
}
int start = ;
int end = A.size() - ;
while (start + < end) {
int mid = start + (end - start) / ;
if (A[mid] == target) {
return mid;
} else if (A[mid] > target) {
end = mid;
} else {
start = mid;
}
}
if (A[end] == target) {
return end;
}
if (A[end] < target) {
return end + ;
}
if (A[start] >= target) {
return start;
} return start + ;
}
};
【LintCode】060.Search Insert Position的更多相关文章
- 【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 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...
- 【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
一天一道LeetCode系列 (一)题目 Given a sorted array and a target value, return the index if the target is foun ...
- 【Lintcode】011.Search Range in Binary Search Tree
题目: Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find a ...
- 【Lintcode】062.Search in Rotated Sorted Array
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- 【Lintcode】038.Search a 2D Matrix II
题目: Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence ...
- 【Lintcode】028.Search a 2D Matrix
题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...
- 60. Search Insert Position 【easy】
60. Search Insert Position [easy] Given a sorted array and a target value, return the index if the t ...
随机推荐
- Enumerate Combination C(k, n) in a bitset
Suppose n<=32, we can enumerate C(k, n), with bits representing absence or presence, in the follo ...
- document.readyState和xmlhttp.onreadystatechange
document.readyState的几种状态 0-uninitialized:XML 对象被产生,但没有任何文件被加载. 1-loading:加载程序进行中,但文件尚未开始解析. 2-loaded ...
- opencv3.3.1 opencv_contribut 3.3.1 git 20180117最新版的在ubuntu1604上的编译
过程: 1. git clone ... contribut 2. git clone ... opencv 3. git checkout -b v3.3.1 4 gi ...
- nginx配置1:借助Nginx搭建反向代理服务器与缓存静态文件
修改配置文件nginx.conf (1)进程数与每个进程的最大连接数: •nginx进程数,建议设置为等于CPU总核心数 •单个进程最大连接数,那么该服务器的最大连接数=连接数*进程数 (2)Ngin ...
- PowerBuilder -- Tab控件
在tab中关闭窗口 Close(tab_1.getparent()) 调整tab中的控件的tab oder 鼠标右键tabpage_1,选择 Tab Order菜单.
- 关于海康视频采集卡的简介---基于pci的插潮采集卡
vga 640x480 qvga vga的1/4,宽高分别是vga的一半 (1)采集类型 海康威视 DS-2CE16A2P-IT3P 700TVL 1/3" DIS ICR 红外防水筒型摄像 ...
- Centos7重新安装yum
Centos7重新安装yum rpm -qa|grep yum 然后用下面的命令删除出现的xxx包: rpm -e --nodeps xxx 下载 python-urlgrabber-3.10-8.e ...
- WePY根据环境变量来改变运行时的参数
WePY根据环境变量来改变运行时的参数 · Tencent/wepy Wiki https://github.com/Tencent/wepy/wiki/WePY%E6%A0%B9%E6%8D%AE% ...
- 我的Android进阶之旅------>Android图片处理(Matrix,ColorMatrix)
本文转载于:http://www.cnblogs.com/leon19870907/articles/1978065.html 在编程中有时候需要对图片做特殊的处理,比如将图片做出黑白的,或者老照片的 ...
- Linux就该这么学--命令集合11(配置系统相关信息)
1.配置主机名称: 查看主机名: hostname 修改主机名: vim /etc/hostname 2.配置网卡信息: 在红帽RHEL6系统中网卡配置文件的前缀为“ifcfg-eth”,第一块即为“ ...