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 ...
随机推荐
- Linux shell 脚本中, $@ 和$# 分别是什么意思?
转自:https://zhidao.baidu.com/question/412833470.html $@:表示所有脚本参数的内容 $#:表示返回所有脚本参数的个数. 示例:编写如下shell脚本, ...
- resin配置
第一种方式: 在本机调试Resin服务,一般是在Eclipse IDE中进行,这样即可以快速的在开发环境中启动/终止Resin,又可以很方便的Debug程序.跟踪异常栈. 在任何版本的 ...
- python之MySQL学习——数据操作
1.增 import pymysql as ps # 打开数据库连接 db = ps.connect(host=', database='test', charset='utf8') # 创建一个游标 ...
- sqlserver----记录转载(行转列)、列转行、pivot、unpivot
CREATE TABLE [StudentScores] ( ), --学生姓名 ), --科目 [Score] FLOAT, --成绩 ) 如果我想知道每位学生的每科成绩,而且每个学生的全部成绩排成 ...
- confirm() event.target.getAttribute('id')
w <?php $wecho = '<form id="del' . $wid . '" method="POST" action="&q ...
- Spring MVC 根据请求方法限定:请求方法(四)
请求方法 说明 GET 使用GET方法检索一个表述(representation)——也就是对资源的描述.多次执行同一GET请求,不会对系统造成影响,GET方法具有幂等性[指多个相同请求返回相同的结果 ...
- pycharm-->github / github-->pycharm
一.pycharm -->发布到--> github:1.github 账号密码 https://github.com/2.git 下载安装 https://git-scm.com/3.配 ...
- yield的表达式形式、面向过程编程(grep -rl 'root' /etc)
一.yield的表达形式 def foo(): print('starting') while True: x=yield None#return 2 print('value :',x) g=foo ...
- 关于var关键字的详解
var 在很多语言中都比较常见,到底var是什么,如何应用,下面就笔者常用的javascript.c#对var进行说明: var 是 variable(变量,可变物)的简写.在多种计算机编程语言中,v ...
- SVM学习笔记(二)----手写数字识别
引言 上一篇博客整理了一下SVM分类算法的基本理论问题,它分类的基本思想是利用最大间隔进行分类,处理非线性问题是通过核函数将特征向量映射到高维空间,从而变成线性可分的,但是运算却是在低维空间运行的.考 ...