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

题目大意:给一个有序数组(无重复元素),一个数字,检查数字是否出现在数组中,出现就返回数组下标,否则返回应插入的位置下标。

解题思路:二分。

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

Search Insert Position——LeetCode的更多相关文章

  1. Search Insert Position [LeetCode]

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

  2. Search Insert Position leetcode java

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

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

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

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

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

  5. [LeetCode] 035. Search Insert Position (Medium) (C++)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...

  6. Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position)

    Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position) 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会 ...

  7. LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)

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

  8. leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version

    704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...

  9. LeetCode: Search Insert Position 解题报告

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

随机推荐

  1. PHP操作Oracle数据库

    原文出处 (这是来自“百度文库”中的文章写得很不错) PHP操作Oracle数据库(OCI数据抽象层)OCI(Oracle 8 Call-Interface)是PHP中内置的数据库抽象层函数.下面针对 ...

  2. $HTTP_RAW_POST_DATA

    这是手册里写的 总是产生变量包含有原始的 POST 数据.否则,此变量仅在碰到未识别 MIME 类型的数据时产生.不过,访问原始 POST 数据的更好方法是 php://input.$HTTP_RAW ...

  3. oracle约束条件状态

    Oracle完整性约束有一下4种: • DISABLE NOVALIDATE • ENABLE NOVALIDATE • DISABLE VALIDATE • ENABLE VALIDATE   •  ...

  4. sublime text 2 笔记

    sublime text 2 ,是代码程序员最佳编辑器,不是之一.其快捷优雅的操作风格,和便利的快捷键,是程序员码农的不二选择. 网上下载sublime text 2,支持文件拖放,文件夹拖放.3.0 ...

  5. IOS开发网络篇之──ASIHTTPRequest详解

    目录 目录 发起一个同步请求 创建一个异步请求 队列请求 请求队列上下文 ASINetworkQueues, 它的delegate提供更为丰富的功能 取消异步请求 安全的内存回收建议 向服务器端上传数 ...

  6. UIAlertController (UIActionSheet, UIAlertView is deprecated in iOS 8.)

    iOS 8 后 UIAlertView 和  UIActionSheet 都被合并到了 UIAlertController里面. 文档原文: Important: UIAlertView is dep ...

  7. 【BZOJ2281】【博弈论+DP】 [Sdoi2011]黑白棋

    Description 黑白棋(game) [问题描述] 小A和小B又想到了一个新的游戏. 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色. 最左边是白色棋子,最右边是 ...

  8. gvim 常用命令

    插入: insert 强退: :q! 退出: :q 保存: :w 保存退出::wq 复制: yy(单行)   多行:8yy 删除: dd(单行)   多行:8dd 或者 :4,8d 执行脚本: :! ...

  9. 新一代的代码编辑神器Sublime Text 3(使用指南)

    首先附上官网下载链接:http://www.sublimetext.com/3 接下来是安装sublime最强大的插件功能:Package Control 一.简单的安装方法 使用Ctrl+`快捷键或 ...

  10. MOS管(场效应管)导通条件

    场效应管的导通与截止由栅源电压来控制,对于增强型场效应管来说,N沟道的管子加正向电压即导通,P沟道的管子则加反向电压.一般2V-4V就可以了.    但是,场效应管分为增强型(常开型)和耗尽型(常闭型 ...