【算法】LeetCode算法题-Search Insert Position
这是悦乐书的第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的更多相关文章
- 乘风破浪:LeetCode真题_035_Search Insert Position
乘风破浪:LeetCode真题_035_Search Insert Position 一.前言 这次的问题比较简单,也没有限制时间复杂度,但是要注意一些细节上的问题. 二.Search Insert ...
- [Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
- 【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 ...
- Leetcode 二分查找 Search Insert Position
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 T ...
- [LC]35题 Search Insert Position (搜索插入位置)
①英文题目 Given a sorted array and a target value, return the index if the target is found. If not, retu ...
- 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 ...
- LeetCode:35. Search Insert Position(Easy)
1. 原题链接 https://leetcode.com/problems/search-insert-position/description/ 2. 题目要求 给定一个已经排好序的数组和一个目标值 ...
- 【一天一道LeetCode】#35. Search Insert Position
一天一道LeetCode系列 (一)题目 Given a sorted array and a target value, return the index if the target is foun ...
- 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 ...
随机推荐
- navicat连接不上Linux服务器上的MySQL
1.首先确定你的linux已经关闭防火墙 详细操作点这里: 如果是公司服务器防火墙比较重要不能关闭,那就麻烦点了,需要在防火墙的配置文件下配置属性. 如果还不能解决,请继续往下看. 2.如果是云服务器 ...
- IdentityServer4客户端JWT解密实现(基于.net4.0)
情景:公司项目基于.net4.0,web客户端实现单点登录需要自己解密id_token,对于jwt解密,.net提供了IdentityModel类库,但是4.0中该类库不可用,所以自己实现了解密方法. ...
- 多个微信小程序一个服务端架构
由于某些特定的业务场景,当多个小程序需要一个服务端后台提供数据时,大家可能想到是HTTP路由.是的,实际上我们使用微服务的GateWay网关也是一样的,如下图微服务架构: 网关GateWay的作用在于 ...
- mysql存储过程调用含out参数
mysql 数据库有以下存储过程: CREATE DEFINER=`root`@`localhost` PROCEDURE `hovertreeTest`( IN `Param1` INT, ), O ...
- python中的property属性
目录 1. 什么是property属性 2. 简单的实例 3. property属性的有两种方式 3.1 装饰器方式 3.2 类属性方式,创建值为property对象的类属性 4. property属 ...
- Hibernate入门(十一)多对多案例
Hibernate多对多案例 1.用户对角色 DROP TABLE IF EXISTS emp_role; DROP TABLE IF EXISTS employee; DROP TABLE IF E ...
- python爬虫scrapy项目详解(关注、持续更新)
python爬虫scrapy项目(一) 爬取目标:腾讯招聘网站(起始url:https://hr.tencent.com/position.php?keywords=&tid=0&st ...
- for循环与forEach性能思考
今日看到一句话: 基于循环的迭代比基于函数的迭代法快8倍,因此有了该篇验证博客. 验证代码如图: 验证结果:在数量比较少的时候,无明显差别,当数量级达到10的4次方时候,for循环的效率优势明显:如图 ...
- OSI 七层,TCP 四层 , TCP 五层模型介绍
以 TCP 四层模型为例,介绍对应的物理设备 传输层: 四层交换机,四层路由器 网络层: 路由器,三层交换机 数据链路层: 网桥,以太网交换机,网卡 物理层: 中继器,集线器,双绞线 各层功能介绍 物 ...
- Vue2 几种常见开局方式
在SF问题中看到了一个关于vue-cli中的template问题,问题是这样的:用vue-cli工具生成的main.js中: import Vue from 'vue' import App from ...