【算法】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 ...
随机推荐
- Redis 初次见面
目录 Redis 特性 使用场景 初次使用 安装(Linux) 配置 启动 redis 的 3 种方法 使用 redis 客户端 关闭 redis 服务 Redis 版本说明 引用 1 Redis 特 ...
- HDUOJ-2089 不要62
Problem Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来 ...
- 爬虫应对js混淆的方法
大家做爬虫可能经常要跟js打交道.如果积累一定的经验肯定会遇到eval(....);这种js,很多新人可能慌了,woc这怎么办??????? 下面楼主给大家介绍一种方法简单,有效. F12 在Cons ...
- mysql+mycat实现读写分离
centos7 master slave mycat1.6 client 192.168.41.10 192.168.41.11 192.168.41.12 192.168.41.13 实 ...
- vim 学习笔记系列(前言)
今天上午的时候,看到大神在用vim编程,画面直观,速度很快,操作只需要用命令符就可以实施. 所以可以推断vim的命令符是复杂的,那么学习过程中记忆会很漫长,很痛苦,但是如果记住了这些命令符,并可以熟练 ...
- Reinforcement Learning: An Introduction读书笔记(2)--多臂机
> 目 录 < k-armed bandit problem Incremental Implementation Tracking a Nonstationary Problem ...
- Java web.xml笔记
Javaweb项目中, web.xml文件其中的各种设置, 就是简单的标注 <?xml version="1.0" encoding="UTF-8"?&g ...
- 微信小程序之发送模板消息(通过openid推送消息给用户)
一.获取access_token access_token是接口调用的凭证,目前有效期为两个小时,需要定时刷新,重复获取将导致上次获取的access_token失效.(注:不建议每次调用需要acces ...
- vue-cli中安装方法
源:http://www.cnblogs.com/jn1223/p/6656956.html vue-cli中安装方法 vue-cli脚手架模板是基于node下的npm来完成安装的所以首先需要安装 ...
- 洛谷P4591 [TJOI2018]碱基序列(hash dp)
题意 题目链接 Sol \(f[i][j]\)表示匹配到第\(i\)个串,当前在主串的第\(j\)个位置 转移的时候判断一下是否可行就行了.随便一个能搞字符串匹配的算法都能过 复杂度\(O(|S| K ...