Hard-题目10:33. Search in Rotated Sorted Array

来源:互联网 发布:大学生如何利用网络 编辑:程序博客网 时间:2024/06/15 07:37

题目原文:
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.
题目大意:
对一个移位过的,无重复数字的数组,寻找目标数字并返回其下标。如果没有则返回-1.
题目分析:
一开始想到的思路是找到跳变点(即移动点),然后两边都是有序的,再二分查找,但是想不到logn复杂度的,就决定直接无脑找了。
源码:(language:java)

public class Solution {    public int search(int[] nums, int target) {        for(int i = 0;i<nums.length;i++)            if(nums[i]==target)                return i;        return -1;    }}

源码:
1ms,beats 5.34%,众数1ms,94.64%

0 0
原创粉丝点击