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

【题目分析】

给定一个已排序的数组,数组中没有重复的数字。给定一个数字找到该数子出现的位置,如果数组中不存在该数字,则返回如果插入该数字应该插入的位置。

【思路】

涉及到已排序的数组,我们一般都会采用二分查找算法。如果目标数字存在,则直接返回结果即可。若不存在呢?

研究一下二分查找,left和right相等时,如果此时的值和目标值相同则left就是应该返回的结果,否则的话我们比较一下目标值和下标left对应的值的大小。如果目标值较大,则返回left+1,否则返回left。

【java代码】

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

LeetCode OJ 35. Search Insert Position的更多相关文章

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

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

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

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

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

  4. LeetCode:35. Search Insert Position(Easy)

    1. 原题链接 https://leetcode.com/problems/search-insert-position/description/ 2. 题目要求 给定一个已经排好序的数组和一个目标值 ...

  5. LeetCode OJ:Search Insert Position(查找插入位置)

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

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

  7. 【LeetCode】35. Search Insert Position 解题报告(Java & Python)

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

  8. 【LeetCode OJ】Search Insert Position

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

  9. LeetCode Problem 35:Search Insert Position

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

随机推荐

  1. android studio Activity标题栏研究

    第一次研究时间:2016/7/30,以下研究主要存在于当前最新版本的android studio上.eclipse请参考 一.头部标题取消 当前版本新建工程在 application中默认主题为 an ...

  2. Unity3DGUI:GUILayout

    显示效果,注意GUILayout控件默认垂直布局,且在水平布局模块里控件大小默认按控件内容来显示,因此对于水平滑块HorizontalSlider来说需要自定义大小避免变形

  3. Android 简单的图片缩放方法

    很简单的一个图片缩放方法,注意要比例设置正确否则可能会内存溢出 相关问题 java.lang.IllegalArgumentException: bitmap size exceeds 32bits ...

  4. CodeForces 707D Persistent Bookcase

    $dfs$,优化. $return$操作说明该操作完成之后的状态和经过操作$k$之后的状态是一样的.因此我们可以建树,然后从根节点开始$dfs$一次(回溯的时候复原一下状态)就可以算出所有状态的答案. ...

  5. ios UIImageView异步加载网络图片

    方法1:在UI线程中同步加载网络图片 UIImageView *headview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 4 ...

  6. SNMP概述–运维必知的协议基础

    一.什么是SNMP?   SNMP是  “Simple Network Management Protocol” 的缩写,中文意思是简单网络管理协议,它是由互联网工作小组在RFC1157中定义的应用层 ...

  7. nginx(1)

    下一篇:nginx(2) 一.engin X 市场的服务器非常的多,这里简单介绍几种常用的. Apache:源代码开放,跨平台,可移植,且支持的模块非常丰富,虽然在速度性能上不如其他轻量级的web服务 ...

  8. fbset视频参数说明

    在机器上输入:fbset mode "1280x720-55"  # D: 67.504 MHz, H: 40.961 kHz, V: 54.907 Hz geometry 128 ...

  9. web.xml Attribute "xmlns" was already specified for element "web-app"

    报错信息:Attribute "xmlns" was already specified for element "web-app" 由于项目的重命名,出现了x ...

  10. JPG 图片在IE下不能显示的问题

    最近碰到一些客户说,我传的产品图片怎么在网站上无法显示啊.图片也是正常的jpg格式呢.    是的,你传的图片是JPG的,但是怎么就显示不出来呢?    你找深圳网站建设的公司给你建了一个网站,然后在 ...