## 一.題目描述

## 二.解題技巧
由于這道題出現了旋轉的情況,即比第一個元素小的元素可能出現在數值的后半段或者不出現。因此,可以考慮采用變種的二分查找,即在比較中間元素與目標之前,先比較第一個元素與目標的關系,這個時候,會出現三種情況:
1.第一個元素剛好等于目標,返回第一個元素的坐標,函數結束;?
2.第一個元素大于目標,那么目標就可能存在被旋轉到數組后面的情況,這個時候,還要比較與數組中間元素的關系,這個時候又會有三種情況:
~~~
a.中間元素大于第一個元素,這個時候,目標可能存在于數組的后半段中,遞歸調用函數,尋找目標的坐標;
b.中間元素等于目標,返回中間元素的坐標,函數結束;
c.中間元素小于第一個元素,這個時候,又可以分為兩種情況進行:
(1).中間元素小于目標元素,那么目標元素可能存在于數組的后半段中,遞歸調用函數,尋找目標的坐標;
(2).中間元素大于目標元素,那么目標元素可能存在于數組的前半段中,遞歸調用函數,尋找目標的坐標;
~~~
3.第一個元素小于目標,這是也有三種情況需要考慮:
~~~
a.中間元素等于目標元素,返回中間元素的坐標,函數結束;
b.中間元素大于第一個元素,這個時候,也有兩種情況要考慮:
(1).中間元素大于目標,那么目標元素可能存在于數組的前半段中,遞歸調用函數,尋找目標的坐標;
(2).中間元素小于目標,那么目標元素可能存在于數組的后半段中,遞歸調用函數,尋找目標的坐標;
c.中間元素小于第一個元素,那么目標元素可能存在于數組的前半段中,遞歸調用函數,尋找目標的坐標;
~~~
當然,還需要考慮數組的元素個數為0,1, 2,的情況,以及對于遞歸的過程中數組的起始位置坐標以及數組中元素的個數,這些才是這道題的難點所在,我也是調試了很久才調通代碼的。
## 三.示例代碼
~~~
// 時間復雜度O(log n),空間復雜度O(1)
#include <iostream>
using namespace std;
class Solution
{
public:
int SearchRotatedSortedArray(int A[], int n, int target)
{
int start = 0;
int end = n;
int middle = start + (end - start) / 2;
while (start != end)
{
if (target == A[middle])
return middle;
if (A[start] < A[middle])
{
if ((target < A[middle]) && (A[start] <= target))
end = middle;
else
start = middle + 1;
}
else
{
if ((target > A[middle]) && (target <= A[end - 1]))
start = middle + 1;
else
end = middle;
}
}
return -1; // 在數組中找不到目標元素時返回-1
}
};
~~~
## 四.體會
這答題的難點在于邊界條件和遞歸過程中的數組的第一個元素的指針設置和數組元素個數的設置上面,邊界條件經常是面試題考查的重點。
- 前言
- 2Sum
- 3Sum
- 4Sum
- 3Sum Closest
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- Search in Rotated Sorted Array
- Remove Element
- Merge Sorted Array
- Add Binary
- Valid Palindrome
- Permutation Sequence
- Single Number
- Single Number II
- Gray Code(2016騰訊軟件開發筆試題)
- Valid Sudoku
- Rotate Image
- Power of two
- Plus One
- Gas Station
- Set Matrix Zeroes
- Count and Say
- Climbing Stairs(斐波那契數列問題)
- Remove Nth Node From End of List
- Linked List Cycle
- Linked List Cycle 2
- Integer to Roman
- Roman to Integer
- Valid Parentheses
- Reorder List
- Path Sum
- Simplify Path
- Trapping Rain Water
- Path Sum II
- Factorial Trailing Zeroes
- Sudoku Solver
- Isomorphic Strings
- String to Integer (atoi)
- Largest Rectangle in Histogram
- Binary Tree Preorder Traversal
- Evaluate Reverse Polish Notation(逆波蘭式的計算)
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Longest Common Prefix
- Recover Binary Search Tree
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Binary Tree Zigzag Level Order Traversal
- Sum Root to Leaf Numbers
- Anagrams
- Unique Paths
- Unique Paths II
- Triangle
- Maximum Subarray(最大子串和問題)
- House Robber
- House Robber II
- Happy Number
- Interlaving String
- Minimum Path Sum
- Edit Distance
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Decode Ways
- N-Queens
- N-Queens II
- Restore IP Addresses
- Combination Sum
- Combination Sum II
- Combination Sum III
- Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Longest Consecutive Sequence
- Word Search
- Word Search II
- Word Ladder
- Spiral Matrix
- Jump Game
- Jump Game II
- Longest Substring Without Repeating Characters
- First Missing Positive
- Sort Colors
- Search for a Range
- First Bad Version
- Search Insert Position
- Wildcard Matching