这是悦乐书的第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. Java线程实现与安全

    目录 1. 线程的实现 线程的三种实现方式 Java线程的实现与调度 2. 线程安全 Java的五种共享数据 保证线程安全的三种方式 前言 本篇博文主要是是在Java内存模型的基础上介绍Java线程更 ...

  2. ORA-28002:the password will expire within 6 days

    1.查看用户的proifle SELECT username,PROFILE FROM dba_users; 2.查看指定概要文件(如default)的密码有效期设置:SELECT * FROM db ...

  3. iOS main.m解析

    在iOS开发中,有一个文件main.m,可能并不是很引起开发的注意.不过,可能在面试过程中,面试官还是有些会问到主函数里面到底做了哪些工作和任务.下面我们主要看一下main.m内部的逻辑. #impo ...

  4. YTKNetwork源码详解

    本篇是第三篇关于网络请求的,将讲述YTKNetwork源码,上述两篇分别讲述AFNetworking源码解析以及结合自己项目封装AFNetworking. AFNetworking源码解析:https ...

  5. .net 后台判断是否要替换

    Response.Write("<script>window.onload=function (){if(confirm(\"该文件已经存在,确定要替换吗吗?\&quo ...

  6. webapi 控制json的字段(key)显示顺序

    使用两个c#的特性: 加在类上的:[DataContract] 加在字段上的:[DataMember(Name = "ResultCode",EmitDefaultValue = ...

  7. Transact-SQL解析和基本的实用语句

    SQL语言 DDL(数据定义语句) DML(数据操作语句) DCL(数据控制语句) DDL 数据定义 操作对象 操作方式 创建 删除 修改 模式 CREATE SCHEMA DROP SCHEMA 表 ...

  8. [Linux] LVS虚拟服务器四层负载均衡

    随着互联网的爆炸性增长及其在我们生活中日益重要的作用,互联网上的流量急剧增加,并且每年以超过100%的速度增长.服务器上的工作负载正在迅速增加,因此服务器很容易在短时间内过载,尤其是对于流行的网站.为 ...

  9. ConstraintLayout使用

    引言 ConstraintLayout是一个ViewGroup,允许您以灵活的方式定位和调整小部件的方法,项目中的布局嵌套问题对项目性能有着不小的威胁,布局能实现扁平化的话会让软件性能得到很大的提升, ...

  10. JavaScript黑客是这样窃取比特币的,Vue开发者不用担心!

    如果你是JavaScript或者区块链开发者,如果你有关注区块链以及比特币,那么你应该听说了比特币钱包Copay被黑客攻击的事情.但是,你知道这是怎么回事吗? 总结 比特币钱包copay依赖event ...