LeetCode033 Search in Rotated Sorted Array

来源:互联网 发布:mac装windows10系统 编辑:程序博客网 时间:2024/05/22 06:33

详细见:leetcode.com/problems/search-in-rotated-sorted-array


Java Solution: github

package leetcode;public class P033_SearchInRotatedSortedArray {public static void main(String[] args) {System.out.println(new Solution().search(new int[] {3, 1}, 1));}/* * 1 ms * 7.60% * 不知道怎么,WA好多次 */static class Solution {    public int search(int[] nums, int target) {    if (nums == null || nums.length == 0)    return -1;    int broken = 1;    while (broken != nums.length) {    if (nums[broken - 1] > nums[broken])    break;    broken ++;    }    if (broken == nums.length)    return getIndex(nums, 0, broken - 1, target);    int ans = getIndex(nums, 0, broken - 1, target);    if (ans != -1)    return ans;    return getIndex(nums, broken, nums.length - 1, target);    }    private int getIndex(int[] nums, int sti, int eni, int val) {    if ((nums[sti] - val) * (nums[eni] - val) > 0)    return -1;    while (sti <= eni) {    int mid = (sti + eni) >> 1;    if (nums[mid] > val)    eni = mid - 1;    else if (nums[mid] < val)    sti = mid + 1;    else    return mid;    }    return -1;    }}}


C Solution: github

/*    url: leetcode.com/problems/search-in-rotated-sorted-array/*/#include <stdio.h>#include <stdlib.h>//[i, j)int _s(int* n, int i, int j, int t) {    int m = 0;    j --;    if (n[i] == t) return i;    else if (j > i && n[j] == t) return j;    while (i <= j) {        m = i + (j - i) / 2;        if (n[m] == t) {            return m;        } else if (n[m] > t) {            j = m - 1;        } else {            i = m + 1;        }    }    return -1;}//[i, j)int _search(int* n, int i, int j, int t) {    int m = 0, a = -1;    j --;    if (i > j) return -1;    if (n[j] < n[i]) {        m = i + (j - i) / 2;        if (n[m] == t) return m;        if (n[m] > n[i]) {            a = _s(n, i, m, t);            if (a != -1) return a;            a = _search(n, m + 1, j, t);        } else {            a = _s(n, m + 1, j, t);            if (a != -1) return a;            a = _search(n, i, m, t);        }        return a;    } else {        return _s(n, i, j + 1, t);    }}int search(int* nums, int numsSize, int target) {    if (numsSize < 1) return -1;    else if (nums[0] == target) return 0;    else if (nums[numsSize - 1] == target) return numsSize - 1;    return _search(nums, 0, numsSize, target);}int main() {    int n [] = {3, 1};    int ns = 2;    int t = 2;    //for (t = -20; t < 21; t ++)        printf("t is %d answer is %d\r\n", t, search(n, ns, t));}


Python Solution: github

#coding=utf-8'''    url: leetcode.com/problems/next-permutation/    @author:     zxwtry    @email:      zxwtry@qq.com    @date:       2017年4月1日    @details:    Solution: 29ms 49.33%'''class Solution(object):    #[i, j)    def binarySearch(self, n, i, j, t):        j -= 1        while i < j:            m = (i + j) // 2            if n[m] > t:                j = m - 1            elif n[m] == t:                return m            else:                i = m + 1        return i if n[i] == t else -1        #[i, j)    def solve(self, n, i, j, t):        j -= 1        while i < j:            m, a = (i + j) // 2, -1            if n[m] == t:                return m            if n[m] > n[i]:                a = self.binarySearch(n, i, m, t)                if a != -1: return a                i = m + 1            else:                a = self.binarySearch(n, m + 1, j + 1, t)                if a != -1: return a                j = m        return i if n[i] == t else -1            def search(self, n, t):        """        :type n: List[int]        :type t: int        :rtype: int        """        nn = 0 if n == None else len(n)        if nn == 0:return -1        return self.solve(n, 0, nn, t)    if __name__ == "__main__":    n = [1,3]    sol = Solution()    print(sol.search(n, 3))        



0 0
原创粉丝点击