国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LintCode/LeetCode] Search in Rotated Sorted Arra

U2FsdGVkX1x / 3621人閱讀

摘要:找中點(diǎn)若起點(diǎn)小于中點(diǎn),說明左半段沒有旋轉(zhuǎn),否則說明右半段沒有旋轉(zhuǎn)。在左右半段分別進(jìn)行二分法的操作。只判斷有無,就容易了。還是用二分法優(yōu)化

Search in Rotated Sorted Array Problem

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.

Example

For [4, 5, 1, 2, 3] and target=1, return 2.

For [4, 5, 1, 2, 3] and target=0, return -1.

Challenge

O(logN) time

Note

找中點(diǎn):若起點(diǎn)小于中點(diǎn),說明左半段沒有旋轉(zhuǎn),否則說明右半段沒有旋轉(zhuǎn)。
在左右半段分別進(jìn)行二分法的操作。

Solution
public class Solution {
    public int search(int[] A, int target) {
        int start = 0, end = A.length-1, mid = 0;
        while (start <= end) {
            mid = (start+end)/2;
            if (A[mid] == target) return mid;
            if (A[start] <= A[mid]) {
                if (A[start] <= target && target <= A[mid]) end = mid-1;
                else start = mid+1;
            }
            else {
                if (A[mid] < target && target <= A[end]) start = mid+1;
                else end = mid-1;
            }
        }
        return -1;
    }
}


Search in Rotated Sorted Array II Problem

Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed?

Would this affect the run-time complexity? How and why?

Write a function to determine if a given target is in the array.

Note

只判斷有無,就容易了。

Solution

還是用二分法優(yōu)化

public class Solution {
    public boolean search(int[] nums, int target) {
        if (nums == null || nums.length == 0) return false;
        int start = 0, end = nums.length-1;
        while (start <= end) {
            int mid = start+(end-start)/2;
            if (nums[mid] == target || nums[start] == target || nums[end] == target) return true;
            if (nums[start] < nums[mid]) {
                if (nums[start] <= target && target < nums[mid]) end = mid-1;
                else start = mid+1;
            }
            else if (nums[start] > nums[mid]) {
                if (nums[mid] < target && target <= nums[end]) start = mid+1;
                else end = mid-1;
            }
            else {
                if (nums[start] != target) start++;
                if (nums[end] != target) end--;
            }
        }
        return false;
    }
}
Updated 2018-08
class Solution {
    public boolean search(int[] nums, int target) {
        int start = 0, mid = 0, end = nums.length-1;
        while (start <= end) {
            mid = start+(end-start)/2;
            if (nums[mid] == target || nums[start] == target || nums[end] == target) return true;
            if (nums[start] < nums[mid]) {
                if (nums[start] < target && target < nums[mid]) end = mid-1;
                else start = mid+1;
            } else if (nums[start] > nums[mid]) {
                if (nums[mid] < target && target < nums[end]) start = mid+1;
                else end = mid-1;
            } else {
                start++;
                end--;
            }
        }
        return false;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/65939.html

相關(guān)文章

  • [LintCode/LeetCode] Find Minimum in Rotated Sorted

    摘要:排序數(shù)組中找最小值或最大值的題目,很明顯可以使用二分法。因此,只判斷終點(diǎn)和中點(diǎn)元素的大小關(guān)系即可。這里有一種情況是上述后三個(gè),中點(diǎn)值和末位相等。此時(shí),兩邊同時(shí)遞歸,并返回兩邊遞歸值的較小值。當(dāng)首位和末位重合,說明已夾逼得到最小值。 Find Minimum in Rotated Sorted Array Problem Suppose a sorted array is rotated...

    cgh1999520 評(píng)論0 收藏0
  • [Leetcode] Search in Rotated Sorted Array 搜索旋轉(zhuǎn)有序數(shù)組

    摘要:如果左邊的點(diǎn)比右邊的大,說明這兩個(gè)點(diǎn)之間有一個(gè)旋轉(zhuǎn)點(diǎn),導(dǎo)致了不再有序。因?yàn)橹挥幸粋€(gè)旋轉(zhuǎn)點(diǎn),所以一分為二后,肯定有一半是有序的。 Search in Rotated Sorted Array I Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 mi...

    thursday 評(píng)論0 收藏0
  • leetcode33 search in rotated sorted array

    摘要:這里相比于思路一,更適用于目標(biāo)節(jié)點(diǎn)在中間的情況,而思路一在目標(biāo)節(jié)點(diǎn)分布在數(shù)組兩側(cè)會(huì)效率更高。 題目要求 Suppose an array sorted in ascending order 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 ...

    MkkHou 評(píng)論0 收藏0
  • leetcode 33 Search in Rotated Sorted Array

    摘要:如正常的升序排列應(yīng)該是,,,,,,旋轉(zhuǎn)過后可能就是,,,,,,。想法因?yàn)檫@是一個(gè)經(jīng)過旋轉(zhuǎn)的升序數(shù)組,我們可以將其看作兩個(gè)升序的序列,,,和,,。如果在前一個(gè)序列,則從前面進(jìn)行查找。如果在后面一個(gè)序列,則從最后一個(gè)元素開始查找。 題目詳情 Suppose an array sorted in ascending order is rotated at some pivot unknown...

    diabloneo 評(píng)論0 收藏0
  • [LintCode/LeetCode] Search Insert Position

    Problem Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume NO duplicates in the a...

    cjie 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<