Search in Rotated Array II

来源:互联网 发布:棋牌软件开发公司 编辑:程序博客网 时间:2024/05/17 00:13

Search in Rotated Array II

给定旋转有序数组,允许重复数字,查找目标数字。

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.

Solution:

    public boolean search(int[] A, int target) {        if (A == null || A.length == 0) {            return false;        }        for (int i = 0; i < A.length; i++) {            if (A[i] == target) {                return true;            }        }        return false;    }
思路:

假设数组为{2,2,2,2,3,  2,2},则其实际可看成为无序,无法分段。

无序则只能遍历,O(n)不可避免。



0 0