【leetcode_medium】34. Find First and Last Position of Element in Sorted Array

leetcode_medium_array

problem

34. Find First and Last Position of Element in Sorted Array

solution #1:

解题思路:分别使用二分法查找最左和最右端的元素;

code

注意,最右端元素的mid值如何确定;

solution #2:

解题思路:使用二分法找到元素,然后进行前后移动,找到最左最右端的元素;

code

solution #3:

解题思路:将target±0.5作为新的目标值,进行搜索;

code

注意:

1. 数组是有序的;

2. 注意最右端元素的mid值如何确定;

3. 注意边界条件的确定;

参考

1. leetcode_34. Find First and Last Position of Element in Sorted Array;

做自己该做的事情,做自己喜欢做的事情,安静做一枚有思想的技术媛。
版权声明,转载请注明出处:https://www.cnblogs.com/happyamyhope/
原文地址:https://www.cnblogs.com/happyamyhope/p/13345324.html