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

转载 2016年05月31日 23:27:13

题目原文:
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%

相关文章推荐

leetcode(33). Search in Rotated Sorted Array

problem Suppose an array sorted in ascending order is rotated at some pivot unknown to you befo...

33. Search in Rotated Sorted Array

//本来想找到队列最小值,再进行二分查找,后来发现直接做也可以,但是if条件比较复杂 class Solution { public: int search(vector& nums, int...

LeetCode OJ 33 Search in Rotated Sorted Array

难度:hard 由此可见leetcode的hard难度比较于acm是偏简单一些的。 Suppose a sorted array is rotated at some pivot unkn...

leetcode 33. 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 mig...

leetcode解题之33&81. Search in Rotated Sorted Array java版(在旋转的数字中查找指定值)

33. Search in Rotated Sorted Array ,81. Search in Rotated Sorted Array II 。 leetcode解题之33&81. Search...

[leetcode] 33. 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 mig...

leetcode 33. 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 mig...

leetcode 33. Search in Rotated Sorted Array

题目大意:一个排序数组绕某个数选择了,在这个旋转后的数组中找指定的元素target 找到就返回在数组中的序号,否则返回-1 解题思路: 1、当然是可以直接搜的(复杂度O(n)),也可以过.但是对于有一...

Leetcode 33. Search in Rotated Sorted Array

题目如下: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand...

Leetcode 33 - Search in Rotated Sorted Array(二分)

题意给一个有序数组,然后从数组的某一个位置,将前后两半交换。现在给一个数k,求k在数组中出现的位置,如果没有出现,返回-1。思路算法1画个图,其实就两种情况:前半段长还是后半段长,然后分若干种情况去讨...
  • Lzedo
  • Lzedo
  • 2017-02-13 16:44
  • 88
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)