【LEETCODE】32、LeetCode的第35题,查找插入的位置
凉凉,看来想做好一个题还不容易啊。。。
有点难受。。。
1.看看题目吧
Given a sorted array and a target value, return the index if the target is found. If not,
return the index where it would be if it were inserted in order. You may assume no duplicates in the array.
给定一个有序数组,依旧是二分查找,不同之处是如果没有找到指定数字,需要返回这个数字应该插入的位置。
本来想想着不就二分查找嘛,分分钟搞定。。。结果悲剧了,小瞧了这题了,活活被气死
2.上代码
废话不多说了,可恶上代码吧
先来看看悲剧版本1:
class Solution {
public int searchInsert(int[] nums, int target) {
//这个题类似之前去小米面试的题,用二分查找
int start = 0, end = nums.length - 1;
int result = -1; while(start < end) {
//注意判断当找到的位置值比target大,那么说明数据在左边,如果比target小,那么数据在右边
int mid = (start +end)/2; if(nums[mid] == target) {
result = mid;
break;
} else if(nums[mid] < target) {
start = mid;
} else {
end = mid;
}
} //最后如果start == end
if(start == end) {
if(start == 0) {
result = 0;
} else if(nums[start] > target) {
//如果这个时候比targe大,那么就是前一位
result = start - 1;
} else {
result = start + 1;
}
} return result;
}
}
简简单单,3分钟写完。。。然后分分钟悲剧。。。
兄弟们,这个玩意是个死循环。。。。
比如:最后start = 0, end = 1; 那么结果就是mid =0,start=0,end=1,MMP停不下来啊
问题在于,我们如果除数为0的话,那么start = mid,然后我们
nums[mid] < target的时候,start=mid 其实就是没变,还是等于start,永远不会达到条件start>=end的情况 悲剧版本2:
class Solution {
public int searchInsert(int[] nums, int target) {
//这个题类似之前去小米面试的题,用二分查找
int start = 0, end = nums.length - 1;
int result = -1; while(start < end) {
//注意判断当找到的位置值比target大,那么说明数据在左边,如果比target小,那么数据在右边
int mid = (start + end) / 2; if(nums[mid] == target) {
result = mid;
break;
} else if(nums[mid] < target) {
start = mid + 1;
} else {
end = mid;
}
} //最后如果start == end
if(start == end) {
if(start == 0) {
result = 0;
} else if(nums[start] > target) {
//如果当前位置比这个值大,那么说明当前的值是用来取代这个位置的
result = start;
} else {
//如果比这个值小,那么就应该往后移一位
result = start + 1;
}
} return result;
}
}
哎,一把辛酸泪,试试nums=[1],val=2吧,你会发现,循环根本没进去,然后start永远==0,那么结果就是0,MMP
行吧,行吧,我把start==0的判断去掉还不行么
悲剧版本3:
class Solution {
public int searchInsert(int[] nums, int target) {
//这个题类似之前去小米面试的题,用二分查找
int start = 0, end = nums.length - 1;
int result = -1; while(start < end) {
//注意判断当找到的位置值比target大,那么说明数据在左边,如果比target小,那么数据在右边
int mid = (start + end) / 2; if(nums[mid] == target) {
result = mid;
break;
} else if(nums[mid] < target) {
start = mid + 1;
} else {
end = mid;
}
} //最后如果start == end
if(start == end) {
if(nums[start] > target) {
//如果当前位置比这个值大,那么说明当前的值是用来取代这个位置的
result = start;
} else {
//如果比这个值小,那么就应该往后移一位
result = start + 1;
}
} return result;
}
}
哎呀,能不能一次到位啊,气死啦。。。。
不说了nums={1},val = 1....
好绝望啊,这个时候这个代码会返回1,为啥,我开始以为while找到了,但是没有返回,然后进入了下面的if判断
我改。。。
悲剧版本4:
class Solution {
public int searchInsert(int[] nums, int target) {
//这个题类似之前去小米面试的题,用二分查找
int start = 0, end = nums.length - 1;
int result = -1; while(start < end) {
//注意判断当找到的位置值比target大,那么说明数据在左边,如果比target小,那么数据在右边
int mid = (start + end) / 2; if(nums[mid] == target) {
return mid;
} else if(nums[mid] < target) {
start = mid + 1;
} else {
end = mid;
}
} //最后如果start == end
if(start == end) {
if(nums[start] > target) {
//如果当前位置比这个值大,那么说明当前的值是用来取代这个位置的
result = start;
} else {
//如果比这个值小,那么就应该往后移一位
result = start + 1;
}
} return result;
}
}
他娘的,其实它while根本就没进去。。。。
绝望*n
哎,其实这里是漏掉了考虑没有进循环,然后start的位置正好就是找到了的位置。。。
改呗。。。
class Solution {
public int searchInsert(int[] nums, int target) {
//这个题类似之前去小米面试的题,用二分查找
int start = 0, end = nums.length - 1;
int result = -1; while(start < end) {
//注意判断当找到的位置值比target大,那么说明数据在左边,如果比target小,那么数据在右边
int mid = (start + end) / 2; if(nums[mid] == target) {
return mid;
} else if(nums[mid] < target) {
start = mid + 1;
} else {
end = mid;
}
} //最后如果start == end
if(start == end) {
if(nums[start] >= target) {
//如果当前位置比这个值大或相等,那么正好是这个位置,那么说明当前的值是用来取代这个位置的
result = start;
} else {
//如果比这个值小,那么就应该往后移一位
result = start + 1;
}
} return result;
}
}
终于过关了。。。不容易啊
不过这里还可以优化一下运算,那就是用位运算取代/运算
class Solution {
public int searchInsert(int[] nums, int target) {
//这个题类似之前去小米面试的题,用二分查找
int start = 0, end = nums.length - 1;
int result = -1; while(start < end) {
//注意判断当找到的位置值比target大,那么说明数据在左边,如果比target小,那么数据在右边
int mid = start + ((end - start) >>> 1); if(nums[mid] == target) {
return mid;
} else if(nums[mid] < target) {
start = mid + 1;
} else {
end = mid;
}
} //最后如果start == end
if(start == end) {
if(nums[start] >= target) {
//如果当前位置比这个值大或相等,那么正好是这个位置,那么说明当前的值是用来取代这个位置的
result = start;
} else {
//如果比这个值小,那么就应该往后移一位
result = start + 1;
}
} return result;
}
}
到这里就差不多了!!
【LEETCODE】32、LeetCode的第35题,查找插入的位置的更多相关文章
- 【LeetCode】- Search Insert Position(查找插入的位置)
[ 问题: ] Given a sorted array and a target value, return the index if the target is found. If not, re ...
- LeetCode第35题:搜索插入位置
题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 输入: [1,3,5,6 ...
- Leetcode春季打卡活动 第二题:206. 反转链表
Leetcode春季打卡活动 第二题:206. 反转链表 206. 反转链表 Talk is cheap . Show me the code . /** * Definition for singl ...
- Leetcode 春季打卡活动 第一题:225. 用队列实现栈
Leetcode 春季打卡活动 第一题:225. 用队列实现栈 Leetcode 春季打卡活动 第一题:225. 用队列实现栈 解题思路 这里用了非常简单的思路,就是在push函数上做点操作,让队头总 ...
- 【LeetCode】34. 在排序数组中查找元素的第一个和最后一个位置
34. 在排序数组中查找元素的第一个和最后一个位置 知识点:数组,二分查找: 题目描述 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置 ...
- 剑指offer 面试35题
面试35题: 题目:复杂链表的复制 题:输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head.(注意,输出结果中 ...
- [LC]35题 Search Insert Position (搜索插入位置)
①英文题目 Given a sorted array and a target value, return the index if the target is found. If not, retu ...
- 使用 MySQL 查找附近的位置
使用 MySQL 查找附近的位置 以下 SQL 语句将会在与坐标 37, -122 相距 25 英里的半径范围内查找最近的 20 个位置.该语句根据行的纬度/经度以及目标纬度/经度计算距离,然后只请求 ...
- python数组查找算法---bisect二分查找插入
1 实例 这个模块只有几个函数, 一旦决定使用二分搜索时,立马要想到使用这个模块 [python] view plaincopyprint? import bisect L = [1,3,3,6,8, ...
随机推荐
- FontAwesome 图标
FontAwesome 图标 前言 FontAwesome 大家都不陌生,精美的图标,出现在各式各样的网页中.最近在做 Windows Forms 应用程序,要求美观,就想能不能把 FontAweso ...
- PHP数组教程
定义数组 PHP数组array是一组有序的变量,其中每个变量被叫做一个元素. 一.定义数组 可以用 array() 语言结构来新建一个数组.它接受一定数量用逗号分隔的 key => value ...
- Tagging Physical Resources in a Cloud Computing Environment
A cloud system may create physical resource tags to store relationships between cloud computing offe ...
- 在WPF中制作正圆形公章
原文:在WPF中制作正圆形公章 之前,我利用C#与GDI+程序制作过正圆形公章(利用C#制作公章 ,C#制作公章[续])并将它集成到一个小软件中(个性印章及公章的画法及实现),今天我们来探讨一下WPF ...
- ZOJ 2319 Beatuiful People(单调递增序列的变形)
Beautiful People Time Limit: 5 Seconds Memory Limit: 32768 KB Special Judge The most prest ...
- httpclient POST请求(urlencoded)
搬砖搬砖~ Content-Type:application/x-www-form-urlencoded的请求如下 var nvc = new List<KeyValuePair<stri ...
- C# 自定义泛型类,并添加约束
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- ASP FirstWeb
//html <!DOCTYPE html> <html><head><meta http-equiv="Content-Type" co ...
- WPF之VirtualizingStackPanel.IsVirtualizing="False"
原文:WPF之VirtualizingStackPanel.IsVirtualizing="False" 相信从winform转到wpf的人都遇到过这样的困惑,在处理DataGri ...
- teamcity build web project arguments
/p:Configuration=%system.Configuration% => Release /p:DeployOnBuild=%system.DeployOnBuild% => ...