这是悦乐书的第152次更新,第154篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第11题(顺位题号是35)。给定排序数组和目标值,如果找到目标,则返回索引。 如果没有,请返回索引按顺序插入的索引。假设数组中没有重复项。例如:

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

输出:2

输入:[1,3,5,6],2

输出:1

输入:[1,3,5,6],7

输出:4

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

输出:0

本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试。

02 第一种解法

首先排除几种特殊情况,然后顺位循环,拿每一个元素与目标值比较。

public int searchInsert(int[] nums, int target) {
if (nums.length == 0 || nums[0] > target) {
return 0;
}
if(nums[nums.length-1] < target){
return nums.length;
}
int result = 0;
for(int i=0; i<nums.length; i++){
if (nums[i] < target) {
result++;
}
if (nums[i] == target) {
return i;
}
}
return result;
}

03 第二种解法

使用二分法快速定位,取中间位索引判断值,直到匹配上。

public int searchInsert2(int[] nums, int target) {
int L = 0;
int R = nums.length-1;
while (true) {
if (target <= nums[L]) {
return L;
}
if (target > nums[R]) {
return R+1;
}
int mid = (L+R)/2;
if (target <= nums[mid]) {
R = mid-1;
}
if (target > nums[mid]) {
L = mid+1;
}
}
}

04 小结

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

【算法】LeetCode算法题-Search Insert Position的更多相关文章

  1. 乘风破浪:LeetCode真题_035_Search Insert Position

    乘风破浪:LeetCode真题_035_Search Insert Position 一.前言 这次的问题比较简单,也没有限制时间复杂度,但是要注意一些细节上的问题. 二.Search Insert ...

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

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

  3. 【LeetCode】35. Search Insert Position (2 solutions)

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

  4. Leetcode 二分查找 Search Insert Position

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 T ...

  5. [LC]35题 Search Insert Position (搜索插入位置)

    ①英文题目 Given a sorted array and a target value, return the index if the target is found. If not, retu ...

  6. LeetCode OJ 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:35. Search Insert Position(Easy)

    1. 原题链接 https://leetcode.com/problems/search-insert-position/description/ 2. 题目要求 给定一个已经排好序的数组和一个目标值 ...

  8. 【一天一道LeetCode】#35. Search Insert Position

    一天一道LeetCode系列 (一)题目 Given a sorted array and a target value, return the index if the target is foun ...

  9. LeetCode OJ:Search Insert Position(查找插入位置)

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

随机推荐

  1. 南大算法设计与分析课程OJ答案代码(4)--变位词、三数之和

    问题 A: 变位词 时间限制: 2 Sec  内存限制: 10 MB提交: 322  解决: 59提交 状态 算法问答 题目描述 请大家在做oj题之前,仔细阅读关于抄袭的说明http://www.bi ...

  2. 结构型---组合模式(Composite Pattern)

    组合模式的定义 组合模式允许你将对象组合成树形结构来表现”部分-整体“的层次结构,使得客户以一致的方式处理单个对象以及对象的组合. 组合模式实现的最关键的地方是——简单对象和复合对象必须实现相同的接口 ...

  3. CSS的简单复习总结

    如果说网页是人的话那么CSS就是化妆品了哈哈哈,所以网页离不开CSS的装饰.id选择器和Class选择器    二者都是用来选择元素进行装饰的,我个人理解是类选择器包含了id选择器,class选择器不 ...

  4. 《Office 365开发入门指南》上市说明和读者服务

    写在最开始的话 拙作<Office 365开发入门指南>上周开始已经正式在各大书店.在线商城上市,欢迎对Office 365的开发.生态感兴趣的开发者.项目经理.产品经理参考本书,全面了解 ...

  5. IE console.log 调试状态

    最近项目遇到问题,发现alert一个弹窗,在IE中,打开开发人员工具后,可以弹出,但是不打开无法弹出,最后发现是console.log的原因,注释掉console相关的代码,问题就解决了 有些版本的I ...

  6. Linux 系统的安装 (最全收集)

    在几年前,我曾经多次萌生抛弃Win系统,从而使用Linux系统-----(Ubuntu),但是我每次都会遇到同一个问题,TM怎么安装啊. 不是安装奇慢就是不知道安装的方法. 怎样安装Ubuntu操作系 ...

  7. Eureka开启登录认证

    Eureka服务端配置 一.Eureka的pom.xml 引入spring-boot-starter-security坐标 <dependency> <groupId>org. ...

  8. wx-charts 微信小程序图表 -- radarChart C# .net .ashx 测试

    radarChart:原始代码 new wxCharts({ canvasId: 'radarCanvas', type: 'radar', categories: ['1', '2', '3', ' ...

  9. vuejs2.0实现分页组件,使用$emit进行事件监听数据传递

    上一篇文章介绍了vuejs实现的简单分页,如果我有几个页面都需要有分页效果,不可能每个页面都去复制一下这段代码吧,意思是封装一下,变成通用的组件. 首先使用基础 Vue 构造器,创建一个“子类”,Vu ...

  10. Suricata规则编写——常用关键字

    本篇转载自:http://blog.csdn.net/wuyangbotianshi/article/details/44775181 1.简介 现在的NIDS领域snort一枝独秀,而suricat ...