Leetcode no.33
2016-04-13 20:01
387 查看
33. Search in Rotated Sorted Array
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.
public class Solution { public int search(int[] nums, int target) { if (nums.length==0) return -1; int start= 0, end= nums.length-1; while (start <= end){ int center= (start+end)/2; if (nums[center] == target) return center; if (nums[center]>= nums[start]){ if (target < nums[center] && target>= nums[start]) end= center-1; else start= center+1; } if (nums[center]<=nums[end]){ if (target > nums[center] && target<=nums[end]) start= center+1; else end= center-1; } } return -1; } }
相关文章推荐
- 菜鸟学python(10) 用python处理Layer导出的域名(将每个域名前加入http://)
- SQL优化的部分内容
- jquery请求servlet实现ajax异步请求
- 最大连续子序列和 HDU 1231 (时间复杂度为O(n))
- 【UFLDL-exercise9-Convolution and Pooling】
- struts2中result跳转jsp时路径问题
- ios可变数组NSMutableArray和不可变数组NSArray的区别
- 【BZOJ3407】[Usaco2009 Oct]Bessie's Weight Problem 贝茜的体重问题【01背包】
- 深入理解Android中的View
- storm源码之理解Storm中Worker、Executor、Task关系
- Hibernate与 MyBatis的比较
- jsp中Servlet处理请求后转到一个jsp页面的解决办法
- BZOJ4337 BJOI2015 树的同构
- Java 压缩zip格式文件
- Hash算法冲突解决方法分析
- nodeJs grunt中Gruntfile.js模版
- java的锁机制
- PHP——超全局变量_学习笔记
- 表单提交方式总结
- KMP 算法