题目描述:

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You may assume no duplicates in the array.

Here are few examples.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0

解题思路:

使用二分法搜索,当出现相等的情况就返回该位置的值;否则结果是right < left;这表明在上一次循环中出现两种情况:1、mid位置的值大于target,则最后一次循环后right = mid - 1,所以插入的位置肯定为right + 1的位置;2、mid位置的值小于target,则最后一次循环后left = mid + 1,所以插入的位置肯定为right + 1。综上,代码如下所示:

代码如下:

public class Solution {
public static int searchInsert(int[] nums, int target) {
int left, right, mid;
left = 0;
right = nums.length - 1; while (left <= right) {
mid = (left + right) / 2;
if (nums[left] == target)
return left;
if (nums[right] == target)
return right;
if (nums[mid] == target)
return mid; if (nums[mid] > target)
right = mid - 1;
else if (nums[mid] < target)
left = mid + 1;
}
return right + 1;
}
}

Java [leetcode 35]Search Insert Position的更多相关文章

  1. [array] leetcode - 35. Search Insert Position - Easy

    leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...

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

  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 35 Search Insert Position(查找插入位置)

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

  5. [leetcode 35] Search Insert Position

    1 题目: Given a sorted array and a target value, return the index if the target is found. If not, retu ...

  6. Leetcode 35 Search Insert Position 二分查找(二分下标)

    基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...

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

  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 ☆(丢失的数字)

    转载:https://leetcode.windliang.cc/leetCode-35-Search-Insert-Position.html    思路 Given a sorted array ...

随机推荐

  1. VS2010中无法嵌入互操作类型“......”,请改用适用的接口的解决方法

  2. iOS 基础 第三天(0807)

    0807 成员变量作用域###### 如下图所示: 这里要注意手写的成员变量/实例变量默认的作用域是private,所以外部指针类型的对象无法直接访问,这起到一定的保护作用.但可以在当前类内部@imp ...

  3. 【BZOJ 2038】[2009国家集训队]小Z的袜子(hose)

    Description HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH不断地收集新的贝壳,因此, 他的项链变 ...

  4. O2O模式

    O2O即Online To Offline(在线离线/线上到线下) 是指将线下的商务机会与互联网结合,让互联网成为线下交易的前台,这个概念最早来源于美国.O2O的概念非常广 泛,既可涉及到线上,又可涉 ...

  5. yum安装gcc

    如果服务器是自己的,并且机器就在身边,那什么都不用说了,缺少gcc顶多就是重新放入安装盘,把开发工具包安装上.但是如果是租的服务器,托管服务 方那帮人又搞不懂你说的啥子gcc,要安装gcc实在是太麻烦 ...

  6. PAT-乙级-1018. 锤子剪刀布 (20)

    1018. 锤子剪刀布 (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 大家应该都会玩“锤子剪刀布”的游 ...

  7. 软件调试之INT 3讲解

    第4章断点和单步执行 断点和单步执行是两个经常使用的调试功能,也是调试器的核心功能.本章我们将介绍IA-32 CPU是如何支持断点和单步执行功能的.前两节将分别介绍软件断点和硬件断点,第4.3节介绍用 ...

  8. hdu 4655 Cut Pieces

    这个解题报告讲的很详细了!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #in ...

  9. hbase 使用备忘

    hbase是基于hadoop的,所以hbase服务器必须启动hadoop,这点很重要. 当然hbase其实只用到了dadoop的一个组件 1. 启动hadoop-dfs 在主上执行如下命令,可以把主和 ...

  10. 镜面电火花EDM加工技术资料,模具行业的人应该好好看看!

    目前镜面电火花加工技术在精密型腔模具制造中逐步得以推广.本文就企业中镜面电火花加工应用的关键环节,结合实践分析了影响镜面加工性能的因素.通过控制各个工艺环节,可有效实现高质量.高效率的镜面电火花加工. ...