①英文题目

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. Windows系统调用中的系统服务表描述符

     Windows内核分析索引目录:https://www.cnblogs.com/onetrainee/p/11675224.html Windows系统调用中的系统服务表描述符 在前面,我们将解过 ...

  2. luogu P4035 [JSOI2008]球形空间产生器

    [返回模拟退火略解] 题目描述 今有 n+1n+1n+1 个 nnn 维的点,它们都在一个球上.求它们所在球的球心. Solution 4035\text{Solution 4035}Solution ...

  3. 初识AutoIt

    1.定义 AutoIt 目前最新是v3版本,这是一个使用类似BASIC脚本语言的免费软件,它设计用于Windows GUI(图形用户界面)中进行自动化操作.它利用模拟键盘按键,鼠标移动和窗口/控件的组 ...

  4. Halcon一日一练:图像分割之基本概念

    1.什么是图像分割: 图像分割就是把图像中特定的目标提出来,进行处理. 2.为什么要做图像分割: 图像分割是由图像处理到图像分析的关键步骤,准确的来说,没有图像分割,图像处理将无法实现其后续的操作.进 ...

  5. g_pLog

     g_pLog = new CLog("log");  g_pLog->Enable();  g_pScrLog = new CLog("data"); ...

  6. git生成SSH秘钥

    1.进入git bash , 输入 cd ~/.ssh/ ,没有的话,自己创建 mkdir ~/.ssh , 然后进入该文件夹完成生成秘钥步骤 2.配置全局的name和email,这里是的你githu ...

  7. 【Spring Cloud】微服务架构选型方案

    1.技术架构 2.组件介绍 1.服务注册与发现——Eureka 服务注册与发现中心采用Eureka,以AP为核心的高可用注册中心,保证高可用性和最终一致性,server之间互相注册的replicate ...

  8. 数据类型(二)---day04

    目录 上节课回顾 五 变量 (一)什么是变量 (二)变量的组成 (三)变量名的命名规范 (四)常量 (五)python变量内存管理 (六)变量的三种打印方式 六 数据类型 (一)数字类型 (二)字符串 ...

  9. Linux下搭建.NetCore3.0环境及创建项目

    ================================================== ================================================= ...

  10. Jsp的四大域对象

    Jsp     Jsp的四大域对象   作用范围 特殊之处   pageContext 当前jsp页面,当转发就失效 可以获取其他域对象中的值   request 一次请求,转发公用request,重 ...