①英文题目

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.

Example 1:

Input: [1,3,5,6], 5
Output: 2
Example 2:

Input: [1,3,5,6], 2
Output: 1
Example 3:

Input: [1,3,5,6], 7
Output: 4
Example 4:

Input: [1,3,5,6], 0
Output: 0

②中文题目

给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。

你可以假设数组中无重复元素。

示例 1:

输入: [1,3,5,6], 5
输出: 2
示例 2:

输入: [1,3,5,6], 2
输出: 1
示例 3:

输入: [1,3,5,6], 7
输出: 4
示例 4:

输入: [1,3,5,6], 0
输出: 0

③ 思路

这就是个遍历查找判决过程,相等就输出当前索引。但是要注意边界问题,

1、target<nums[0]时,输出索引是0.

2、target>nums[nums.length]时,输出索引为nums.length。

3、有一个地方要特别注意,第8行代码,如果不先保证i+1<nums.length,那么nums[i+1]会越界。

④代码

 class Solution {
public int searchInsert(int[] nums, int target) {
int k = Integer.MIN_VALUE;
//int k = Integer.MIN_VALUE;
for(int i=0;i<nums.length;i++){
if(nums[i]==target)
k=i;
if(i+1<nums.length&&nums[i]<target&&nums[i+1]>target)
k=i+1;
}
if(nums[0]>target)
k=0;
if(nums[nums.length-1]<target)
k=nums.length;
return k;
}
}

⑤今天我

Runtime: 0 ms, faster than 100.00% of Java online submissions for Search Insert Position.
Memory Usage: 38.8 MB, less than 100.00% of Java online submissions for Search Insert Position.

耗时0ms,击败了100%的用java的人,尽管这个程序很简单,但是还是很高兴。该午休了,下午去实验室。

[LC]35题 Search Insert Position (搜索插入位置)的更多相关文章

  1. lintcode:Search Insert Position 搜索插入位置

    题目: 搜索插入位置 给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引.如果没有,返回到它将会被按顺序插入的位置. 你可以假设在数组中无重复元素. 样例 [1,3,5,6],5 → 2 ...

  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] 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(搜索插入位置)

    这道题是LeetCode里的第35道题. 题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元 ...

  5. 035 Search Insert Position 搜索插入位置

    给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引.如果没有,返回到它将会被按顺序插入的位置.你可以假设在数组中无重复元素.案例 1:输入: [1,3,5,6], 5输出: 2案例 2:输 ...

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

  7. 【LeetCode每天一题】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算法题-Search Insert Position

    这是悦乐书的第152次更新,第154篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第11题(顺位题号是35).给定排序数组和目标值,如果找到目标,则返回索引. 如果没有, ...

  9. [Leetcode] search insert position 寻找插入位置

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

随机推荐

  1. Redis学习三(进阶功能).

    一.排序 redis 支持对 list,set 和 zset 元素的排序,排序的时间复杂度是 O(N+M*log(M)).(N 是集合大小,M 为返回元素的数量) sort key [BY patte ...

  2. plSql使用流程

    1. 下载PLSQL developer.instantclient_11_2, 下载地址:https://pan.baidu.com/s/1_MjmIT4nUzsQ7Hi8MCrs1A, 备注:此安 ...

  3. Linux下mqttServer搭建

    1.apache-apollo 创建服务 tar -zxvf apache-apollo-1.7-unix-distro.tar.gz cd /home/bk/apache-apollo-/bin . ...

  4. gperftools::TCMalloc

    VS2013编译gperftools-2.4 1)https://github.com/gperftools/gperftools  下载   gperftools-2.4.zip  版本.2)解压  ...

  5. Python斐波那契数列

    今天偶然看到这个题目,闲着没事练一下手 if __name__ == '__main__': """ 斐波那契数列(Fibonacci sequence), 又称黄金分割 ...

  6. photometric_stereo halcon光度立体法三维表面重建

    官方文档翻译 名称: photometric_stereo -- 通过光度立体技术重建表面. 签名: photometric_stereo(Images : HeightField, Gradient ...

  7. TCC推导过程

    svn 账号 yuanzn 密码:TCH5mb 项目分层 MapperDao 数据校验 throw new CloudBaseRuntimeException Helper 数据转换 manager ...

  8. linux 下ln命令--笔记

    linux 下ln命令 ln命令用来为文件创建连接,连接类型分为硬连接和符号连接两种,默认的连接类型是硬连接.如果要创建符号连接必须使用"-s"选项.注意:符号链接文件不是一个独立 ...

  9. 博客文章编辑器 Cmd Markdown

    欢迎使用 Cmd Markdown 编辑阅读器 编辑器点击打开链接 学习语言地址点击打开链接

  10. 学习笔记23_AspMVC项目

    *创建AspMVC项目 (1)会自动创建App_Data文件夹,是用户不能访问和下载的.一般用户能访问那些文件夹,可以在IIS中配置. (2)App_Start文件夹,用于放置与程序有关的配置文件. ...