Description:

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(target <= nums[0]) {
return 0;
} if(target > nums[nums.length-1]) {
return nums.length;
} for(int i=0; i<nums.length; i++) {
if(target <= nums[i]) {
return i;
} } return nums.length; }
}

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

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

  2. LeetCode: Search Insert Position 解题报告

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

  3. [LeetCode] 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] Search Insert Position 二分搜索

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

  5. [LeetCode] 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 Search Insert Position Python

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

  7. LeetCode Search Insert Position (二分查找)

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

  8. [Leetcode] 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 Search Insert Position (二分查找)

    题意: 给一个升序的数组,如果target在里面存在了,返回其下标,若不存在,返回其插入后的下标. 思路: 来一个简单的二分查找就行了,注意边界. class Solution { public: i ...

随机推荐

  1. [pthread]Linux C 多线程简单示例

    #include <stdio.h> #include <pthread.h> pthread_mutex_t mutex; pthread_cond_t cond; void ...

  2. JAVA的包装类2 【转】

    怎么理解JAVA中的“包装类” JAVA是一种面向对象语言,java中的类把方法与数据连接在一起,构成了自包含式的处理单元.但在JAVA中不能定义基本类型(primitive  type)对象,为了能 ...

  3. (转)windows环境vue+webpack项目搭建

    首先,vue.js是一种前端框架,一般利用vue创建项目是要搭配webpack项目构建工具的,而webpack在执行打包压缩的时候是依赖node.js的环境的,所以,要进行vue项目的开发,我们首先要 ...

  4. Android中新建的文件在R没有显示相应的文件解决的方法总结

    我们有时会发现当我们新建一个文件或者目录在Generated Java Files(gen)文件的R.java中没有生成相应的静态类或者静态属性.此时你可能出现了以下的情况.注意參照解决. 一般正常情 ...

  5. if no 和 if not

    x = None if x is not None: print("kong") if not x is not None: print("kong") # i ...

  6. 教你如何做一个优雅的Ecmascripter /转

    看看这些被同事喷的JS代码风格你写过多少 殷荣桧 JavaScript 今天 现在写代码比以前好多了,代码的格式都有eslint,prettier,babel(写新版语法)这些来保证,然而,技术手段再 ...

  7. java-数据库连接工具类 DataSourceUtil.java

    DataSourceUtil.java package com.gordon.utils; import java.sql.Connection; import java.sql.ResultSet; ...

  8. npm常用命令汇总

    npm是一个node包管理和分发工具,已经成为了非官方的发布node模块(包)的标准. 有了npm,可以很快的找到特定服务要使用的包,进行下载.安装以及管理已经安装的包. 1.npm install ...

  9. AJAX 实时读取输入文本(php)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. jQuery方法笔记

    .clone() $(selector).clone(includeEvents) $(this).clone(true) //boolean值,true/false分别对饮是否复制元素的所有事件处理