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使用Home键后应用程序重启的问题

    正常情况应该是在点击 home 按键后,程序在后台暂停运行,点击 后退键 才会退出应用的,但是今天遇到个问题,点击 home 键后,重新再打开应用却每次都返回应用启动页面,有些莫名其妙,一番googl ...

  2. NYOJ 299

    (前言:这是一道关于矩阵快速幂的问题,介绍矩阵快速幂之前,首先看"快速幂"问题. 在前面的博客里有记录到快速幂取模算法,不过总体的思想总是和取模运算混淆在一起,而忽略了" ...

  3. kuangbin专题一 简单搜索

    弱菜做了好久23333333........ 传送门: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105278#overview A ...

  4. 【转】【Egit】如何将eclipse中的项目上传至Git

    1.下载egit插件 打开Eclipse,git需要eclipse授权,通过网页是无法下载egit的安装包的.在菜单栏依次打开eclipse→help→install new software→add ...

  5. Ubuntu 12.04 修改键盘映射

    背景: (1) 我的笔记本G450上,Page_up/Page_down键分别和Home/End在同一个键位上,需要同时按住Fn键才能敲出Home/End (2) 习惯用Vim的同志都有这个感觉,Es ...

  6. Chapter 17_1 弱引用table

    Lua采用了自动内存管理.所以不用担心新创建的对象需要的内存如何分配出来,也不用考虑对象不再被使用后怎样释放它们所占用的内存. Lua实现了一个增量标记-扫描收集器.它使用这两个数字来控制垃圾收集循环 ...

  7. kubernetes port nodePort targetPort 理解

    port The port that the service is exposed on the service's cluster ip (virsual ip). Port is the serv ...

  8. Linux lsof命令详解和使用示例【转】

    所以如传输控制协议 (TCP) 和用户数据报协议 (UDP) 套接字等,系统在后台都为该应用程序分配了一个文件描述符,无论这个文件的本质如何,该文件描述符为应用程序与基础操作系统之间的交互提供了通用接 ...

  9. Oralce生成前N年的年数据

    今天做一个统计报表的时候正好碰到这个问题,原来,一般是通过后台代码来生成.现在直接通过oracle来生成,记录一下. 方法一: SELECT YEAR FROM ( , UNION SELECT TO ...

  10. 如何在IIS6,7中部署ASP.NET网站(转载)

    查看web.config文件 web.config通常会放在网站的根目录,这个文件中包含了一最重要的网站运行参数.比如: connectionStrings,httpHandlers,httpModu ...