【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 ...
随机推荐
- mongodb distinct去重
MongoDB的destinct命令是获取特定字段中不同值列表.该命令适用于普通字段,数组字段和数组内嵌文档. mongodb的distinct的语句: db.users.distinct('last ...
- 【NOI2015】【程序自己主动分析】【并查集+离散化】
Description 在实现程序自己主动分析的过程中,经常须要判定一些约束条件能否被同一时候满足. 考虑一个约束满足问题的简化版本号:如果x1,x2,x3,-代表程序中出现的变量.给定n个形如xi= ...
- 用python实现入门级NLP
今天看到一篇博文,是讲通过python爬一个页面,并统计页面词频的脚本,感觉蛮有意思的 Python NLP入门教程:http://python.jobbole.com/88874/ 本文简要介绍Py ...
- 使用 Django1.11搭建blog项目
使用Django搭建blog项目 简单设置: http://blog.csdn.net/w_e_i_/article/details/70761604 模板渲染: http://blog.csdn.n ...
- Asp.Net中判断是否登录,及是否有权限?
不需要在每个页面都做判段, 方法一:只需要做以下处理即可 using System; using System.Collections.Generic; using System.Linq; usin ...
- java读取TXT文件中的数据
将文件放在一个指定的磁盘目录下: File file = new File("指定的文件路径"); try{ BufferedReader br = new BufferedRea ...
- linux批量更改权限
用命令 sudo chmod 777 -Rfv /home/name/* 注释:1.777 为 要修改成 的 文件的 权限:2.-R 是 子目录 下的 文件 也修改:3.-f 强制:4. -v是 显示 ...
- Matlab图像处理(02)-图像基础
数据类 Matlab中和IPT中支持的基本数据类型如下: 名称 描述 double 双精度浮点数,范围-10308~10308 8字节 uint8 无符号1字节整数,范围[0, 255] uint1 ...
- eclipse(myeclipse) author的默认名字
更改eclipse(myeclipse) author的默认名字 --- 修改MyEclipse eclipse 注释的作者 在eclipse/myeclipse中,当我们去添加注释的作者选项时,@a ...
- 在datax之前版本中添加filewriter并创建job时出现问题
问题描述: