Search insert position, 查找插入位置
问题描述:给定一个有序序列,如果找到target,返回下标,如果找不到,返回插入位置。
算法分析:依旧利用二分查找算法。
public int searchInsert(int[] nums, int target)
{
return binarySearch(nums, 0, nums.length - 1, target);
}
public int binarySearch(int[] nums, int left, int right, int target)
{
int mid = (left + right)/2;
if(left > right)
{
return left;
}
if(nums[mid] == target)
{
return mid;
}
else if(nums[mid] < target)
{
return binarySearch(nums, mid + 1, right, target);
}
else
{
return binarySearch(nums, left, mid - 1, target);
}
}
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 ...
- [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 ...
- [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 ...
- lintcode:Search Insert Position 搜索插入位置
题目: 搜索插入位置 给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引.如果没有,返回到它将会被按顺序插入的位置. 你可以假设在数组中无重复元素. 样例 [1,3,5,6],5 → 2 ...
- 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(搜索插入位置)
这道题是LeetCode里的第35道题. 题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元 ...
- 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 ...
随机推荐
- [LintCode] 带重复元素的排列
递归实现: class Solution { public: /** * @param nums: A list of integers. * @return: A list of unique pe ...
- Spring Security OAuth2 授权失败(401) 问题整理
Spring Cloud架构中采用Spring Security OAuth2作为权限控制,关于OAuth2详细介绍可以参考 http://www.ruanyifeng.com/blog/2014/0 ...
- 160627、你想知道的关于JavaScript作用域的一切
JavaScript中有许多章节是关于scope的,但是对于初学者来说(甚至是一些有经验的JavaScript开发者),这些有关作用域的章节既不直接也不容易理解. 这篇文章的目的就是为了帮助那些想更深 ...
- 网络模型+三次握手+四次挥手+DNS+HTTPS
网络模型+三次握手+四次挥手+DNS+HTTPS 这篇文章十分精华,所以整理一下: 一.网络模型 OSI七层模型,和TCP/IP五层模型(更为普遍) TCP/IP 协议集: 二.TCP协议(传输层)建 ...
- Minecraft Forge编程入门一 “环境搭建”
什么是Forge Minecraft Forge is a Minecraft application programming interface (API) which allows almost ...
- 巨蟒python全栈开发-第14天 内置函数2 递归 二分查找
一.今日内容总览 1.内置函数补充 repr() 显示出字符串的官方表示形式 chr() arscii码中的字,转换成位置 ord() arscii码中的位置,转换成字2.递归 自己调用自己 两个口: ...
- Java中堆内存与栈内存分配浅析
Java把内存划分成两种:一种是栈内存,另一种是堆内存.在函数中定义的一些基本类型的变量和对象的引用变量都是在函数的栈内存中分配,当在一段代码块定义一个变量时,Java就在栈中为这个变量分配内存空间, ...
- elasticsearch数据迁移——elasticsearch-dump使用
先安装好nodejs和nodejs的包管理工具npm.然后安装elasticsearch-dump: npm install elasticdump 下面迁移数据: 先在目的地址创建一个index来储 ...
- shader常用
1 模型空间转裁剪空间 UnityObjectToClipPos(v.vertex) 2 模型空间转世界空间 mul( unity_ObjectToWorld, v.vertex ) 3 雾三件套 U ...
- Android “swipe” vs “fling”
onFling will get executed when a user makes a "fling" motion, and said motion has a veloci ...