【LeetCode】Search Insert Position(搜索插入位置)
这道题是LeetCode里的第35道题。
题目描述:
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
你可以假设数组中无重复元素。
示例 1:
输入: [1,3,5,6], 5
输出: 2示例 2:
输入: [1,3,5,6], 2
输出: 1示例 3:
输入: [1,3,5,6], 7
输出: 4示例 4:
输入: [1,3,5,6], 0
输出: 0
二分查找,简单快捷。每一次将被查找区间分为两块,然后再与该区间的中间值比较,根据比较的结果判断是左边还是右边。
解题代码:
class Solution {
public static int find(int[] nums, int start, int end, int target) {
if(start >= end) {
if(target <= nums[start])return start;
else return start + 1;
}
int half = (start+end)/2;
if(target == nums[half])return half;
else if(target > nums[half])return find(nums, half + 1, end, target);
else return find(nums, start, half - 1, target);
}
public static int searchInsert(int[] nums, int target){
if(nums.length == 0)return 0;
//if(nums.length == 1)return find(nums, 0 ,0, target);
int half = (nums.length)/2;
if(target == nums[half])return half;
else if(target > nums[half])return find(nums, half, nums.length - 1, target);
else return find(nums, 0, half - 1, target);
}
}
提交结果:

个人总结:
这道题算是二分查找的入门题。
【LeetCode】Search Insert Position(搜索插入位置)的更多相关文章
- [LeetCode] Search Insert Position 搜索插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- lintcode:Search Insert Position 搜索插入位置
题目: 搜索插入位置 给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引.如果没有,返回到它将会被按顺序插入的位置. 你可以假设在数组中无重复元素. 样例 [1,3,5,6],5 → 2 ...
- [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] search insert position 寻找插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 035 Search Insert Position 搜索插入位置
给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引.如果没有,返回到它将会被按顺序插入的位置.你可以假设在数组中无重复元素.案例 1:输入: [1,3,5,6], 5输出: 2案例 2:输 ...
- LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- LeetCode: Search Insert Position 解题报告
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- [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每天一题】Search Insert Position(搜索查找位置)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
随机推荐
- PHP snippets
Friendly file size string public static function bytesToSize($bytes) { if ($bytes < 1024) { retur ...
- cesium 加载TMS影像(已经切片)
TMS影像数据格式 加载影像的代码: var layers = viewer.scene.imageryLayers; var blackMarble = layers.addImageryProvi ...
- IOS storyboard(控件器的 生命周期)
@interface NJTwoViewController () @end @implementation NJTwoViewController // 当控制器的view加载完毕就调用 - (vo ...
- POJ 2718 Smallest Difference(dfs,剪枝)
枚举两个排列以及有那些数字,用dfs比较灵活. dfs1是枚举长度短小的那个数字,dfs2会枚举到比较大的数字,然后我们希望低位数字的差尽量大, 后面最优全是0,如果全是0都没有当前ans小的话就剪掉 ...
- linux 查看帐号创建时间
查看用户的home目录的创建时间 查看日志 用stat 命令,可以看到目录的三个时间.不过这个时间只是用来参考的,确定一个范围. 查看日志是最准确的方法 /var/log/auth.log ,前提是你 ...
- 回归树的原理及Python实现
大名鼎鼎的 GBDT 算法就是用回归树组合而成的.本文就回归树的基本原理进行讲解,并手把手.肩并肩地带您实现这一算法. 1. 原理篇 1.1 最简单的模型 如果预测某个连续变量的大小,最简单的模型之一 ...
- python_16_自己建立模块
import python_5_password
- java实现微信扫一扫详解
java实现微信扫一扫详解 一.微信JS-SDK参数配置及查找 JS安全域名配置(查找:微信公众号里-公众号设置-功能设置页) 注:1.安全域名外网必须可以访问的到 2.域名不能有下划线 3.要将 ...
- MyISAM 和 InnoDB 的区别与优化
MyISAM 和 InnoDB 的基本区别 1.InnoDB不支持FULLTEXT类型的索引. 2.InnoDB 中不保存表的具体行数,也就是说,执行select count(*) from tabl ...
- 抽屉head部分,hover应用,鼠标放上变色
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...