1. 原题链接

https://leetcode.com/problems/search-insert-position/description/

2. 题目要求

给定一个已经排好序的数组和一个目标值,假设该数组中没有重复值,返回目标值在数组中的插入位置下标。

3. 解题思路

利用折半查找法定位插入的位置

4. 代码实现

 public class SearchInsertPosition35 {
public static void main(String[] args) {
int[] nums = {1, 3, 5, 6};
System.out.println(searchInsert(nums, 7));
} public static int searchInsert(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (nums[mid] == target) return mid;
else if (nums[mid] > target) right = mid - 1;
else left = mid + 1;
}
return left;
}
}

LeetCode:35. Search Insert Position(Easy)的更多相关文章

  1. Leetcode No.35 Search Insert Position(c++实现)

    1. 题目 1.1 英文题目 Given a sorted array of distinct integers and a target value, return the index if the ...

  2. [Leetcode][Python]35: Search Insert Position

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...

  3. 【LeetCode】35. Search Insert Position (2 solutions)

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

  4. 【一天一道LeetCode】#35. Search Insert Position

    一天一道LeetCode系列 (一)题目 Given a sorted array and a target value, return the index if the target is foun ...

  5. LeetCode:14. Longest Commen Prefix(Easy)

    1. 原题链接 https://leetcode.com/problems/longest-common-prefix/description/ 2. 题目要求 给定一个字符串数组,让你求出该数组中所 ...

  6. LeetCode:12. Roman to Integer (Easy)

    1. 原题链接 https://leetcode.com/problems/roman-to-integer/description/ 2. 题目要求 (1)将罗马数字转换成整数:(2)范围1-399 ...

  7. LeetCode 35 Search Insert Position(查找插入位置)

    题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description   在给定的有序数组中插入一个目标数字,求出插入 ...

  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】35. Search Insert Position 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...

随机推荐

  1. 关于项目中的DAL数据接入层架构设计

    摘要:项目中对关系型数据库的接入再寻常不过,也有海量的ORM工具可供选择,一个一般性的DAL数据接入层的结构却大同小异,这里就分享一下使用Hibernate.Spring.Hessian这三大工具对D ...

  2. dom4j.jar有什么作用?

    om4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个非常非常优秀的Java XML API,具有性能优异.功能强大和极端易用使用的特点,同时它也是一个开放源代 ...

  3. 可变长度参数以及foreach循环原理

    语法糖 接下来几篇文章要开启一个Java语法糖系列,所以首先讲讲什么是语法糖.语法糖是一种几乎每种语言或多或少都提供过的一些方便程序员开发代码的语法,它只是编译器实现的一些小把戏罢了,编译期间以特定的 ...

  4. 一个关于JSON的异常,获取List对象失败的。。。

    重要的事情放在最前面,,以后不管遇到什么异常都一定要把异常读懂再想办法怎么解决,把异常读懂,异常读懂...... 这个异常我记得以前遇到过,而且好像已经做了笔记,,,,,今天翻了一下竟然没有,,,,, ...

  5. Unity3D Shaderlab 学习记录

    unity3d 定制的表面着色器(Surface Shader)的标准输出结构是这种: struct SurfaceOutput  {  half3 Albedo; //反射率  half3 Norm ...

  6. 静态路由解决双外卡,PC做路由器的实现

    1,曾经医院,有两个网卡,一个内,一个外,但都有网关(192.168.1.246. 192. 168.6.1) 这样同一时候开启就会出现网络不通. 当时并没有细究原因. 这次医院信息化项目上马,我学到 ...

  7. 1spring注解:@Configuration,@Bean,@ComponentScan(),@Scope

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...

  8. mybatis分页PageHelper插件的使用

    1.jar包, 2.改mybatis的配置文件,加上这段配置: <plugins><plugin interceptor="com.github.pagehelper.Pa ...

  9. git提交项目

    https://www.cnblogs.com/java-maowei/p/5950930.html

  10. java并发编程之CompletionService

    应用场景当向Executor提交多个任务并且希望获得它们在完成之后的结果,如果用FutureTask,可以循环获取task,并调用get方法去获取task执行结果,但是如果task还未完成,获取结果的 ...