日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

LeetCode--Search in Rotated Sorted Array

發布時間:2025/3/20 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。