【算法】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 ...
随机推荐
- tomcat编译项目后,classes文件没有相应的改变;
tomcat编译项目后,classes文件没有相应的改变: tomcat不能将项目部署到服务器: 1.首先,在tomcat安装目录webapps中将编译后的整个项目删掉,然后再在eclipse将tom ...
- lua的table元类
Lua中提供的元表是用于帮助Lua数据变量完成某些非预定义功能的个性化行为,如两个table的相加.假设a和b都是table,通过元表可以定义如何计算表达式a+b.当Lua试图将两个table相加时, ...
- YTKNetwork网络封装
本篇是答应在端午写iOS网络-四篇源码解析以及封装的最后一篇,是针对上一篇YTKNetwork源码解析后的一次封装,也是自己实际项目中所使用过的.在对YTKNetwork封装的时候,还是需要对YTKN ...
- PHP开发中bcscale timezone charset的设定
关于php的开发,有几个细节设定,需要知悉下:在项目的init.php 或 index.php 或 api.php 1. bcscale(18); 表示bc函数,默认小数点位数. 没有设定的话,默认为 ...
- Linux-read 命令(20)
Linux read 命令 参数说明: -a 后跟一个变量,该变量会被认为是个数组,然后给其赋值,默认是以空格为分割符. -d 后面跟一个标志符,其实只有其后的第一个字符有用,作为结束的标志. -p ...
- ajax 跨域请求解决方案
1.为什么出现跨域: 前端和后端同一个项目下,ajax请求的地址是localhost同一个端口是话,是不会出现跨域问题的,所以相反前端和后端分开时,ajax请求的地址或者端口不是跟后台相同时就会出现跨 ...
- 7.通用程序设计_EJ
第45条: 将局部变量的作用域最小化 该条目与第13条(使类和成员的可访问性最小)本质上是类似的.要使局部变量的作用域最小化,最有利的方法就是在第一次使用它的地方声明.在每个局部变量的声明处都应该包含 ...
- 异步是javascript的精髓
最近做了一个智能家居的APP,目前纯JS代码已经4000多行,不包括任何引入的库.还在不断升级改造中...这个项目到处都是异步.大多数都是3-4层调用.给我的感觉就是异步当你习惯了,你会发现很爽.下面 ...
- 为啥JQuery被淘汰了?
摘要: 技术进步永不止步. 原文:jQuery的没落和技术发展的一般规律 作者:凌霄光 Fundebug经授权转载,版权归原作者所有. jQuery的成就 jQuery是一个伟大的库, 它解决了dom ...
- 用函数式编程对JavaScript进行断舍离
译者按: 当从业20的JavaScript老司机学会函数式编程时,他扔掉了90%的特性,也不用面向对象了,最后发现了真爱啊!!! 原文: How I rediscovered my love for ...