35. 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
public class Solution {
public int searchInsert(int[] nums, int target) {
int len=nums.length;
for(int i =0;i<len;i++){
if(nums[i]==target){
return i;
}
else if(nums[i]<target&&i+1<len&&nums[i+1]>target){
return i+1;
}
else if(nums[len-1]<target){
return len;
}
else if(nums[0]>target){
return 0;
}
}
return 0;
}
}
解题思路:
- 判断边界值最小返回0最大返回数组长度,和某个值一样返回该值下标+1,在两个元素之间返回较大元素下标
- 注意在两个元素之间的时候判断防止溢出
// version 1: find the first position >= target
public class Solution {
public int searchInsert(int[] A, int target) {
if (A == null || A.length == 0) {
return 0;
}
int start = 0, end = A.length - 1; while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (A[mid] == target) {
return mid;
} else if (A[mid] < target) {
start = mid;
} else {
end = mid;
}
} if (A[start] >= target) {
return start;
} else if (A[end] >= target) {
return end;
} else {
return end + 1;
}
}
} // version 2: find the last position < target, return +1, 要特判一下target小于所有数组里面的元素 public class Solution {
public int searchInsert(int[] A, int target) {
if (A == null || A.length == 0) {
return 0;
}
int start = 0;
int end = A.length - 1;
int mid; if (target < A[0]) {
return 0;
}
// find the last number less than target
while (start + 1 < end) {
mid = start + (end - start) / 2;
if (A[mid] == target) {
return mid;
} else if (A[mid] < target) {
start = mid;
} else {
end = mid;
}
} if (A[end] == target) {
return end;
}
if (A[end] < target) {
return end + 1;
}
if (A[start] == target) {
return start;
}
return start + 1;
}
}- 以上两个代码为两种更高效的方法
35. Search Insert Position【leetcode】的更多相关文章
- 60. Search Insert Position 【easy】
60. Search Insert Position [easy] Given a sorted array and a target value, return the index if the t ...
- [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 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 ...
随机推荐
- mysql left join
MySQL左连接不同于简单连接.MySQL LEFT JOIN提供该表额外字段在左侧. 如果使用LEFT JOIN,得到的所有记录的匹配方式相同, 在左边表中得到的每个记录不匹配也会有一个额外的记录. ...
- abelkhan服务器框架
abelkhan是一个开源的游戏服务器框架.目标是提供一个稳定.高效.可扩展的服务器框架. github:https://github.com/qianqians/abelkhan 论坛:http:/ ...
- Mathematica学习笔记2
导入文件中的矩阵 mat = Import["...", "Table"] 转化为向量矩阵(元素为数对) data = Table[{mat[[i, j]], ...
- usaco 2002 月赛 Fiber Communications 题解
Description Farmer John wants to connect his N (1 <= N <= 1,000) barns (numbered 1..N) with a ...
- English Learning - Vampire bats
" Vampire bats are very adaptable." Bambi said. And when their roosts are disrupted or the ...
- OpenGL的配置与搭建
一.项目中所使用的OpenGL扩展库有: 1. freeglut-3.0.0 2. glew-2.0.0 3. glm-0.9.7.3 二.添加扩展库文件 在工程项目文件夹里面添加OpenGLExte ...
- C#获取数据库连接字符
有两种用法:1)using System.Configuraiton; string ConStr=ConfigurationManager.ConnectionStrings["ConSt ...
- dbgrid数据显示和数据源不同
dbgrid数据显示和数据源不同,在ODBC配置时如下设置,去掉勾
- Ant部署(linux)
1.下载 mkdir /opt/ant cd /opt/ant wget http://mirror.bit.edu.cn/apache//ant/binaries/apache-ant-1.9.4- ...
- 【使用WCF,发布服务端浏览报错】未能从程序集“System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089” 中加载类型 “System.ServiceModel.Activation.HttpModule”。
问题: 在WIN7中的IIS服务器中部署WCF服务程序时,通过浏览器访问报出如下错误: 未能从程序集"System.ServiceModel, Version=3.0.0.0, Cultur ...