LeetCode 33 Search in Rotated Sorted Array 二叉查找(三)
来源:互联网 发布:qq软件乐园 编辑:程序博客网 时间:2024/06/05 23:00
题目:
https://leetcode.com/problems/search-in-rotated-sorted-array/
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7
might 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.
想当年校招的时候做过这一题,而且不是写在纸上的。故事是这样的。。。。(以上省略1万字,大意就是面试官拿着他的Thinkpad扔给了我问会写JAVA吗?会用eclipse吗?好,打开eclipse写吧,于是乎我就开始写,自己造了几个case都pass了,面试官一看总感觉哪里不对,狂试好几个,终于试出来个bug)两年过去了,现在我正拿着该公司的macbook pro重新练习着这道题。。。依然写了几遍才过。。。哎
又说多了,大概解法就是需要找到有序的一半是在哪里,找到了之后就看target是否在有序的那一半的范围内:
public class Solution { public int search(int[] A, int target) { if (A == null || A.length == 0) { return -1; } int l = 0; int r = A.length - 1; while (l <= r) { int mid = (l + r) / 2; if (A[mid] == target) { return mid; } else if (A[mid] < A[r]) { // right side is sorted if (target >= A[mid] && target <= A[r]) { l = mid + 1; } else { r = mid - 1; } } else { // left side is sorted if (target >= A[l] && target <= A[mid]) { r = mid - 1; } else { l = mid + 1; } } } return -1; }}
0 0
- LeetCode 33 Search in Rotated Sorted Array 二叉查找(三)
- leetcode---search-in-rotated-sorted-array---查找
- LeetCode 33 Search in Rotated Sorted Array (二分查找 推荐)
- [LeetCode 33]Search in Rotated Sorted Array (二分查找)
- leetcode-33-Search in Rotated Sorted Array 查找
- leetcode 33. Search in Rotated Sorted Array(二分查找)
- LeetCode(33)Search in Rotated Sorted Array
- [leetcode 33] Search in Rotated Sorted Array
- leetcode|33|Search in Rotated Sorted Array
- Leetcode 33 Search in Rotated Sorted Array
- Leetcode[33]-Search in Rotated Sorted Array
- leetcode 33 -- Search in Rotated Sorted Array
- Search in Rotated Sorted Array - LeetCode 33
- leetcode 33 Search in Rotated Sorted Array
- leetcode 33: Search in Rotated Sorted Array
- Leetcode #33 Search in Rotated Sorted Array
- LeetCode #33 Search in Rotated Sorted Array
- Leetcode#33||Search in Rotated Sorted Array
- 统计学习
- LeetCode-Longest Substring Without Repeating Characters
- Thread 的使用方法
- 原文:http://unruledboy.cnblogs.com/archive/2004/06/27/18988.aspx
- 数学基础:向量
- LeetCode 33 Search in Rotated Sorted Array 二叉查找(三)
- Logistic Regression -- 单自变量
- div 浮层遮盖 flash 问题再研究
- 在linux中添加ftp用户,并设置相应的权限
- 最新的Myeclipse 8.6注册码 有效期到2016年10月
- 安装和使用PHP进程管理框架 QPM
- Jackson转换工具(json)
- 设计模式之装饰模式
- Jackson解析工具简单应用