LeetCode--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(未思考:直接做,仔細考慮各種情況來分析)
package leetcode;
public class SearchinRotatedSortedArray {
?? ?public static int search(int[] nums, int target) {
?? ??? ?int index = -1;?? ?
?? ??? ?if (nums.length < 1 || nums == null)
?? ??? ??? ?return -1;
?? ??? ?if (nums.length == 1) {
?? ??? ??? ?if (target == nums[0])
?? ??? ??? ??? ?return 0;
?? ??? ??? ?return -1;
?? ??? ?}?? ??? ?
?? ??? ? if (target == nums[0]) {
?? ??? ??? ?index = 0;
?? ??? ?} else if (target == nums[nums.length - 1]) {
?? ??? ??? ?index =? nums.length - 1;
?? ??? ?}
?? ??? ?
?? ??? ?if(nums[0]<nums[nums.length-1]){
?? ??? ?
?? ??? ?index = Erfen(target,nums);
?? ??? ??? ??? ?
?? ??? ?}else{
?? ??? ?
?? ??? ?if (target < nums[0] && target > nums[nums.length - 1]) {
?? ??? ??? ?index = -1;
?? ??? ?} else if (target > nums[0]) {
?? ??? ??? ?int i = 0;
?? ??? ??? ?while(nums[i]<=nums[i+1] && i < nums.length - 1){
?? ??? ??? ??? ?if(nums[i]==target){
?? ??? ??? ??? ??? ?index = i;
?? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?i++;
?? ??? ??? ?}if(target == nums[i]){
?? ??? ??? ??? ?index=i;
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ?} else if (target < nums[nums.length - 1]) {
?? ??? ??? ?int j = nums.length-1;
?? ??? ??? ?while(nums[j-1]< nums[j] && j > 0){
?? ??? ??? ??? ?if(nums[j]==target){
?? ??? ??? ??? ??? ?index = j;
?? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?j--;
?? ??? ??? ?}
?? ??? ??? ?if(target == nums[j]){
?? ??? ??? ??? ?index=j;
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ?}
?? ?}
?? ??? ?return index;
?? ?}
?? ?public static int Erfen(int target, int[] array) {
?? ??? ?int left = 0;
?? ??? ?int right = array.length - 1;
?? ??? ?while (left < right-1 ) {
?? ??? ??? ?int mid = left + (right - left) / 2;
?? ??? ??? ?if(target >= array[mid]){
?? ??? ??? ??? ?left = mid;
?? ??? ??? ?}else{
?? ??? ??? ??? ?right = mid;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?if(target == array[left]){
?? ??? ??? ?return left;
?? ??? ?}else if(target == array[right]){
?? ??? ??? ?return right;
?? ??? ?}else {
?? ??? ??? ?return -1;
?? ??? ?}
?? ?}
?? ?public static void main(String[] args) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?int[] array = { 3,4,5,6,7,8,9 };
?? ??? ?System.out.println(search(array,5));?
?? ?}
}
2(借鑒網絡,思考后,在數組上設定左右標,利用二分法+遞歸)
思路:在整個數組中,先二分,找到有序的那一邊然后在有序的一邊判斷是否含有target;如果沒有,繼續在無序的一半二分,在有序的一邊查找
package leetcode;
public class newSearchinRotatedSortedArray {
?? ?public static int search(int[] nums, int target) {
?? ?return search(nums,0,nums.length-1,target);
?? ?}
?? ?public static int search(int[] nums, int left, int right, int target) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?if(left > right) return -1;
?? ??? ?
?? ??? ?int mid = (left+right)/2;
?? ??? ?//78123456
?? ??? ?if(nums[mid]==target){
?? ??? ??? ?return mid;
?? ??? ?}
?? ??? ?if(nums[left]<=nums[mid]){
?? ??? ??? ?if(target<=nums[mid]&&target>=nums[left]) {
?? ??? ??? ??? ?return search(nums,left,mid-1,target);????????????????????? //二分法加上“=”號!當使用二分法時,可以在每個判斷大小的地方加“=”?
?? ??? ??? ?}else{
?? ??? ??? ??? ?return search(nums,mid+1,right,target);
?? ??? ??? ?}
?? ??? ?}else if(nums[mid]<=nums[nums.length-1]){
?? ??? ??? ?if(target>=nums[mid]&&target<=nums[right]){
?? ??? ??? ??? ?return search(nums,mid+1,right,target);
?? ??? ??? ?}else{
?? ??? ??? ??? ?return search(nums,left,mid-1,target);
?? ??? ??? ?}
?? ??? ?}
?? ??? ?return -1;
?? ?}
?? ?public static void main(String[] args) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?int[] array = { 7,8,9,3,4,5,6 };
?? ??? ?System.out.println(search(array,3));?
?? ?}
}
轉載于:https://www.cnblogs.com/neversayno/p/5277304.html
總結
以上是生活随笔為你收集整理的LeetCode--Search in Rotated Sorted Array的全部內容,希望文章能夠幫你解決所遇到的問題。