【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 ...
随机推荐
- 创建1M-1T的虚拟磁盘(内存盘)——使用破解版 Primo Ramdisk Server Edition v5.6.0
破解版 Primo Ramdisk Server Edition v5.6.0下载: https://pan.lanzou.com/i0sgcne 步骤: 下载并解压后安装“Primo.Ramdisk ...
- hiho一下 第三十八周 二分答案
题目链接:http://hihocoder.com/contest/hiho38/problem/1 ,挺难想的解题思路,好题. 按照提示的算法来: 我们需要找什么? 在这个题目中我们需要找的是路径最 ...
- pat乙级1050螺旋矩阵
1.用vector建立二维数组: vector<vector<int>> arr(rows); ; i < rows; i++) arr[i].resize(cols); ...
- NET_Framework_4.0installer.rar
部署提示: 1.首先下载有关的安装程序 NET_Framework_4.0installer.rar 这是我整理好的四个软件(大致一共10MB),分别如下 WindowsInstaller-KB893 ...
- 用virtualenv构建一个新的python环境,这个新的环境在这个创建的文件夹下
http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143271210830032 ...
- 使用VSCode搭建TypeScript开发环境 (重点)
下载TypeScript 在CMD(Windows系统)或者终端(macOS系统)中输入一下命令: npm install -g typescript 下载VSCode VSCode是我使用过最棒的编 ...
- "segmentation fault " when "import tensorflow as tf"
https://github.com/tensorflow/tensorflow/issues/2034
- C#Json数据交互
问题:写项目时,难免会遇到前台和后台要进行数据交换,往前台传一个对象或一个对象集,往后台传一个对象,一个对象集.怎么传,你当然不能直接去传递一个对象或对象集,我们可以利用JSON数据相互之间传值. J ...
- Map和Set -----JavaScript
本文摘要:http://www.liaoxuefeng.com/ JavaScript的默认对象表示方式{}可以视为其他语言中的Map或Dictionary的数据结构,即一组键值对. 但是JavaSc ...
- leetcode笔记(一)309. Best Time to Buy and Sell Stock with Cooldown
题目描述 (原题目链接) Say you have an array for which the ith element is the price of a given stock on day i. ...