这道题是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(搜索插入位置)的更多相关文章

  1. [LeetCode] Search Insert Position 搜索插入位置

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  2. lintcode:Search Insert Position 搜索插入位置

    题目: 搜索插入位置 给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引.如果没有,返回到它将会被按顺序插入的位置. 你可以假设在数组中无重复元素. 样例 [1,3,5,6],5 → 2 ...

  3. [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 ...

  4. [Leetcode] search insert position 寻找插入位置

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  5. 035 Search Insert Position 搜索插入位置

    给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引.如果没有,返回到它将会被按顺序插入的位置.你可以假设在数组中无重复元素.案例 1:输入: [1,3,5,6], 5输出: 2案例 2:输 ...

  6. 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 ...

  7. LeetCode: Search Insert Position 解题报告

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  8. [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 ...

  9. 【LeetCode每天一题】Search Insert Position(搜索查找位置)

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

随机推荐

  1. python3.7 安装 xlrd 模块---Mac

    要用Excel将数据和代码分离,需要import xlrd, 使用前需要先安装xlrd模块. 说明:通过在google中搜索“xlrd Mac”,下载xlrd.py模块(下载地址:http://mac ...

  2. Redis单机数据库

    单机数据库 ·Redis服务器的所有数据库都保存在redisServer.db数组中,而数据库的数量则由redisServer.dbnum属性保存. ·客户端通过修改目标数据库指针,让它指向redis ...

  3. 工作流性能优化(敢问activiti有扩展性?)(1)

    工作流待办(首页待办列表),加载缓慢,activiti本机,看了代码又是全部数据加载到内存,然后代码过滤,我为什么又说又呢? 用VisualVM做性能测试:   之前同事给的解决方案: 1.把&quo ...

  4. World Wind Java开发之一(转)

    http://blog.csdn.net/giser_whu/article/details/40477235 参照<World wind Java三维地理信息系统开发指南随书光盘>以及官 ...

  5. [动态规划] uestc oj A - 男神的礼物

    A - 男神的礼物 Time Limit: 3000/3000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Lweb学长 ...

  6. vuejs组件的重要选项

    new Vue({ el:'#demo', data:{ message:'Hello vue.js!' } }) 我们看到这个括号里面包含了很多中间的选项,小括号里面其实是一些参数,这些参数指定了实 ...

  7. python_15_os

    import os #1. os.system('dir') #2 cmd_res=os.system('dir')#执行命令不保存结果 print("-------",cmd_r ...

  8. vs2015“当前不会命中断点 还没有为该文档加载任何符号”的解决方法

    解决方法:工具-选项-调试 -(启用“仅我的代码”)勾去掉

  9. 如何查看连接到手机热点的ip地址

    因为最近玩树莓派,需要手机做热点,然后用树莓派连接到这个热点上,苦于不知道树莓派被分配了什么样的ip地址,经过一番探索,我发现了两种办法, 安装一个 android terminal( 安卓命令行), ...

  10. Python基础:条件与循环

    条件语句 除了 boolean 类型的数据,条件判断最好是显性的 if i != 0: ... 而不是只写出变量名: if i: ... For循环与While循环 通常来说,如果你只是遍历一个已知的 ...