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. Java对象表示方式1:序列化、反序列化的作用

    1.序列化是的作用和用途 序列化:把对象转换为字节序列的过程称为对象的序列化. 反序列化:把字节序列恢复为对象的过程称为对象的反序列化. 对象的序列化主要有两种用途: 1) 把对象的字节序列永久地保存 ...

  2. 折腾apt源的时候发生的错误

    在折腾Ubuntu源的时候,把新的源替换进去,然后 sudo apt-get update 之后报错: W: Unknown Multi-Arch type 'no' for package 'com ...

  3. SOJ1029 Humble Numbers (枚举)

    A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, ...

  4. CAAnimation 动画支撑系统

    Model支撑:(依附对象) 从presentLayer获取数据: 渲染树为私有: -(void)addAnimation:(CAAnimation *)anim forKey:(NSString * ...

  5. java三大特性(封装、继承、多态)

    oop(面向对象程序设计)具有三大特性:封装.继承.多态 一.封装 封装就是讲类的信息隐藏在类的内部,不允许外部程序直接访问,而是通过该类的实现隐藏信息的操作和访问. 实现封装 1.需要修改属性的访问 ...

  6. P2916 [USACO08NOV]安慰奶牛Cheering up the Cow

    往奶牛里打气 题目评级不难. 感觉思路有值得借鉴的地方.(虽然少,毕竟积沙成塔吗qwq) 很容易看出来,是要求最小生成树的. 然后生成树的计算方式不一样. 我们考虑拼接(感觉大部分oi都可以使用类似的 ...

  7. POJ 1644 分苹果 (递归解法)

    把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法. Input 第一行是测试数据的数目t(0 <= t < ...

  8. if else 和 switch的效率

    switch在判断分支时,没有判断所有的可能性,而是用一个静态表来解决这个问题,所以速度要比if-else快. 但是,switch对较复杂的表达式进行判断,所以当我们需要判断一些简单数值时,用swit ...

  9. Sublime text3学习小记(macOS系统下的安装使用)

    [注:以下内容参考https://blog.csdn.net/stilling2006/article/details/54376743] 一.认识Sublime text 1.一款跨平台代码编辑器, ...

  10. Oracle 触发器(一)

    1)触发器是一种特殊的存储过程,触发器一般由事件触发并且不能接受参数,存储器由语句块去调用:触发器是当某个事件发生时自动地隐式运行. 2)触发器分类: 1.DML触发器: 创建在表上,由DML事件引发 ...