LeetCode *** 33. Search in Rotated Sorted Array
2016-04-15 11:00
351 查看
题目:
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e.,
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
分析:
和之前那个题一毛一样。。
代码:
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e.,
0 1 2 4 5 6 7might become
4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
分析:
和之前那个题一毛一样。。
代码:
class Solution { public: int search(vector<int>& nums, int target) { if(nums.size()<1)return -1; int low=0,high=nums.size()-1,mid; while(low<high){ mid=(low+high)/2; if(nums[mid]==target)return mid; if(nums[mid]>nums[high]){ if(nums[mid]>target&&nums[low]<=target)high=mid; else low=mid+1; } else if(nums[mid]<nums[high]){ if(nums[mid]<target&&nums[high]>=target)low=mid+1; else high=mid; }else high--; } if(nums[low]==target)return low; else return -1; } };
相关文章推荐
- 使用maven的profile和filter插件管理配置项
- jdk默认提供的类加载器
- 图片压缩生成bitmap工具
- 相互观看与评价
- 工具栏
- xcode7.3 iTunes Store operation failed问题
- php 实现简单的登录
- php 实现简单的登录
- 送给队友的汉堡包
- arcgis engine 监听element的添加、更新和删除事件(使用IGraphicsContainerEvents)
- 根据终端的分辨率.进行跳转。
- PAT (Basic Level) Practise (中文)1024. 科学计数法 (20)
- Sublime Text 3 快捷键总结(拿走)
- 计算机图形学(二)输出图元_2_ OpenGL画线函数
- Java的引用数据类型及应用
- Dialog设置全屏
- python学习笔记(接口自动化框架 V2.0)
- SVN常见问题及相关原因,供各位查阅
- pjsip(2.4.5)增加视频功能
- [AngularJS] EasyModal - alert - confirm - modal