①英文题目

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. 教你用Vue写一个开心消消乐

    之前做过一个算法题,算法要求就是写一个开心消消乐的逻辑算法,当时也是考虑了一段时间才做出来.后来想了想,既然核心算法都有了,能不能实现一个开心消消乐的小游戏呢,于是花了两天时间做了一个小游戏出来. 效 ...

  2. 朋友外包干了5年java,居然不知道dubbo-monitor是怎么用的?

    Dubbo工具--dubbo-monitor监控平台的发布和使用 1)下载 https://github.com/alibaba/dubbo/archive/dubbo-2.5.8.zip 2)编译 ...

  3. 手把手教你搭建HEXO免费博客

    一.环境搭建 node安装 百度搜索node,进入官网.下载稳定版: 下载好后直接打开安装 我这里将其安装在D盘(可以自己选择安装位置) 可以看到安装包中已经自带npm包管理工具 等待安装完成后,WI ...

  4. 浑身尖刺的服务可用性守护者——hystrix熔断器实践记录

    netflix公司的产品hystrix(长满刺的豪猪),在高可用目标下具有一定熔断.限流.降级的作用.这里主要写一些自己在使用时的问题解决思路,原理请自行理解,包括线程池与信号量模式等. 注意三个参数 ...

  5. 数据结构1_java---单链表的操作,约瑟夫问题

    我们经常实用c++来建立链表,为了学习的方便,此处我使用java实现了对链表的增删改查功能 整个过程较为简单.仅供参考 流程: (1)通过内部类Node建立结点,内部变量作为指针域和数据域,并写下构造 ...

  6. opencv实践::对象计数

    问题描述 真实案例,农业领域经常需要计算对象个数 或者在其它领域拍照自动计数,可以提供效率,减低成本 解决思路 通过二值分割+形态学处理+距离变换+连通区域计算 #include <opencv ...

  7. iguana::json/xml 序列化框架

    环境:win10  vs2017  c++17  boost 1.下载源码:https://github.com/qicosmos/iguana 2.创建工程,包含源码目录.boost库目录:boos ...

  8. 从Go语言编码角度解释实现简易区块链

    区块链技术 人们可以用许多不同的方式解释区块链技术,其中通过加密货币来看区块链一直是主流.大多数人接触区块链技术都是从比特币谈起,但比特币仅仅是众多加密货币的一种. 到底什么是区块链技术? 从金融学相 ...

  9. javascript学习 first-day

    1.javascript是一种客户端语言,设计它的目的是在用户的机器上而不是服务器上执行任务. 1.1 javascript不允许写服务器机器上的语言:   1.2 Javascript不能关闭不是由 ...

  10. 在npm上发布自己的vue组件库(使用npm install 或者 CDN的方式引用)

    一.npm publish发布包到npm库的命令是npm publish npm publish发布包,需要先配置webpack.json文件,如果没有webpack.json文件,可以通过npm i ...