//二分查找后,向两边扩展,二分错了两次,现在是对的。
//还有就是vector可以用{}直接赋值很棒 class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int left = ;
int right = nums.size()-;
int mid = -;
bool flag = false;
while(left<=right){
mid = (left+right)/;
if(nums[mid] == target){flag = true;break;}
else if(nums[mid] < target){left = mid+;}
else{right = mid-;}
}
if(flag == false) return {-,-}; int begin = mid;
while(begin >= ){
if(nums[begin] != target) break;
begin--;
}
begin++; int end = mid;
while(end < nums.size()){
if(nums[end] != target)break;
end++;
}
end--;
return {begin,end};
}
};
//递归的二分
int search(vector<int>& nums, int left, int right, int target) {
if (left > right) return -;
int mid = left + (right - left) / ;
if (nums[mid] == target) return mid;
else if (nums[mid] < target) return search(nums, mid + , right, target);
else return search(nums, left, mid - , target);
}

Leetcode 34的更多相关文章

  1. [array] leetcode - 34. Search for a Range - Medium

    leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...

  2. Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)

    本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...

  3. [LeetCode] 34. Find First and Last Position of Element in Sorted Array 在有序数组中查找元素的第一个和最后一个位置

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  4. [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)

    原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an ...

  5. leetcode@ [34] Search for a Range (STL Binary Search)

    https://leetcode.com/problems/search-for-a-range/ Given a sorted array of integers, find the startin ...

  6. LeetCode 34. Search for a Range (找到一个范围)

    Given an array of integers sorted in ascending order, find the starting and ending position of a giv ...

  7. (二分查找 拓展) leetcode 34. Find First and Last Position of Element in Sorted Array && lintcode 61. Search for a Range

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  8. LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)

    题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description   Problem: 在已知递减排序的数组中,查找到给定 ...

  9. Java实现 LeetCode 34 在排序数组中查找元素的第一个和最后一个位置

    在排序数组中查找元素的第一个和最后一个位置 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n ...

  10. [leetcode 34] search for a range

    1 题目: Given a sorted array of integers, find the starting and ending position of a given target valu ...

随机推荐

  1. PHP对象在内存中的分配

    对像在PHP 里面和整型.浮点型一样,也是一种数据类,都是存储不同类型数据用的, 在运行的时候都要加载到内存中去用,那么对象在内存里面是怎么体现的呢?内存从逻辑上 说大体上是分为4 段,栈空间段.堆空 ...

  2. Django查询orm的前一天,前一周,一个月的数据

    利用datatime模块的datetime.timedelta()方法 计算时间差,以下是用法 唯一要注意的是数据库存储models.datefield字段是日期格式,所以比较的数据也是日期格式 #当 ...

  3. AOP切点表达式

    Aspectj切入点语法定义 在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" 例如定义切入点表达式  execu ...

  4. DrawLayout使用侧滑抽屉

    布局:fg_left_drawer <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...

  5. Linux下Rsync+Inotify-tools实现数据实时同步

    Linux下Rsync+Inotify-tools实现数据实时同步 注意:下面的三个案例都是rsync 每次都是全量的同步(这就坑爹了),而且 file列表是循环形式触发rsync ,等于有10个文件 ...

  6. 一些动态规划问题的java实现

    从上往下推公式,从下往上求解值. 一:矩阵链乘法,最小括号化方案,动态规划方程. 0        如果i=j m[i,j]  ={ min[i,k]+m[k+1,j]+pi-1pkpj        ...

  7. tcp/udp/socket 端口映射,转发小工具

    1) 利用 Python 的 Socket 端口转发,用于远程维护 https://github.com/knownsec/rtcp 2) 反向端口转发工具 Reverse TCP Port to U ...

  8. hdu5057 分块处理,当数值大于数据范围时树状数组 真是巧 将大数据分为小数据来处理

    这题说的给了100000个数有100000次操作 询问 L和R 区间内 在D位上为P的个数,用树状数组存 要开[10][10][100000]的int 开不了但是能开 这么大的unsign short ...

  9. Python 以指定列宽格式化字符串

    问题: 有一些长字符串,想以指定的列宽将他们重新格式化 解决方案: 使用textwrap模块来格式字符串的输出. textwrap 模块对于字符串打印时非常有用的,特别是当希望输出自动匹配终端大小的时 ...

  10. 逆向及BOF基础实践

    逆向及BOF基础实践 20145316 许心远 一.缓冲区溢出基础知识 缓冲区溢出是一种非常普遍.非常危险的漏洞,在各种操作系统.应用软件中广泛存在.利用缓冲区溢出攻击,可以导致程序运行失败.系统宕机 ...