凉凉,看来想做好一个题还不容易啊。。。

有点难受。。。

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题,查找插入的位置的更多相关文章

  1. 【LeetCode】- Search Insert Position(查找插入的位置)

    [ 问题: ] Given a sorted array and a target value, return the index if the target is found. If not, re ...

  2. LeetCode第35题:搜索插入位置

    题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 输入: [1,3,5,6 ...

  3. Leetcode春季打卡活动 第二题:206. 反转链表

    Leetcode春季打卡活动 第二题:206. 反转链表 206. 反转链表 Talk is cheap . Show me the code . /** * Definition for singl ...

  4. Leetcode 春季打卡活动 第一题:225. 用队列实现栈

    Leetcode 春季打卡活动 第一题:225. 用队列实现栈 Leetcode 春季打卡活动 第一题:225. 用队列实现栈 解题思路 这里用了非常简单的思路,就是在push函数上做点操作,让队头总 ...

  5. 【LeetCode】34. 在排序数组中查找元素的第一个和最后一个位置

    34. 在排序数组中查找元素的第一个和最后一个位置 知识点:数组,二分查找: 题目描述 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置 ...

  6. 剑指offer 面试35题

    面试35题: 题目:复杂链表的复制 题:输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head.(注意,输出结果中 ...

  7. [LC]35题 Search Insert Position (搜索插入位置)

    ①英文题目 Given a sorted array and a target value, return the index if the target is found. If not, retu ...

  8. 使用 MySQL 查找附近的位置

    使用 MySQL 查找附近的位置 以下 SQL 语句将会在与坐标 37, -122 相距 25 英里的半径范围内查找最近的 20 个位置.该语句根据行的纬度/经度以及目标纬度/经度计算距离,然后只请求 ...

  9. python数组查找算法---bisect二分查找插入

    1 实例 这个模块只有几个函数, 一旦决定使用二分搜索时,立马要想到使用这个模块 [python] view plaincopyprint? import bisect L = [1,3,3,6,8, ...

随机推荐

  1. wpf控件开发基础(3) -属性系统(2)

    原文:wpf控件开发基础(3) -属性系统(2) 上篇说明了属性存在的一系列问题. 属性默认值,可以保证属性的有效性. 属性验证有效性,可以对输入的属性进行校验 属性强制回调, 即不管属性有无发生变化 ...

  2. jvm常用优化方案和方法

    新生代 GC(Minor GC):指发生在新生代的垃圾收集动作,因为 Java 对象大多都具备朝生夕灭的特性,所以 Minor GC 非常频繁,一般回收速度也比较快. 老年代 GC(Major GC ...

  3. win10 uwp 使用 asp dotnet core 做图床服务器客户端

    原文 win10 uwp 使用 asp dotnet core 做图床服务器客户端 本文告诉大家如何在 UWP 做客户端和 asp dotnet core 做服务器端来做一个图床工具   服务器端 从 ...

  4. EasyUI学习之menu and button(菜单和按钮)

    前言 今天下午的天气感觉格外的气闷,整个人有一种黏糊糊的感觉,格外的不舒服.加之立即要放假了了,感觉自己全然坐不住呢(节前综合症么).只是学习还是的继续的. 原定计划这篇文章本来应该是关于search ...

  5. HDU 4279 Number(2012天津网络游戏---数论分析题)

    转载请注明出处:http://blog.csdn.net/u012860063? viewmode=contents 题目链接:pid=4279">http://acm.hdu.edu ...

  6. MongoDB Shell 经常使用的操作

    数组查询 数组查询 MongoDB 中有子文档的概念,一个文档中能方便的嵌入子文档,这与关系性数据库有着明显的不同.在查询时,语法有一些注意点. 样例代码,假如我们的一个集合(tests)中存在标签键 ...

  7. 【转载】Docker部署nginx并修改配置文件

    docker 部署个nginx docker run \ --name nginx-health-web-pc \ -d -p 6800:80 \ -v /usr/docker/nginx/html: ...

  8. jquery li练习

    <!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  9. WPF——TargetNullValue(如何在绑定空值显示默认字符)

    原文:WPF--TargetNullValue(如何在绑定空值显示默认字符) 说明:在数据绑定时,如果有些字段为空值,那么在数据绑定时可以用默认值来显示为空的字段. </Grid> { L ...

  10. Hamcrest、Mockito 专题

    UT中需要的jar Junit4.1X.jar hamcrest-library-1.x.jar hamcrest-core-l.x.jar mockito-all-1.10.x.jar Junit ...