L2:05 - Search in Rotated Sorted Array II

来源:互联网 发布:淘宝助理是干什么的 编辑:程序博客网 时间:2024/05/25 23:26

https://oj.leetcode.com/problems/search-in-rotated-sorted-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.

Linear algorithm, go for-loop!


/** * Copyright: NineChapter * - Algorithm Course, Mock Interview, Interview Questions * - More details on: http://www.ninechapter.com/ */// it ends up the same as sequential search// We used linear search for this question just to indicate that the // time complexity of this question is O(n) regardless of binary search is applied or not.public class Solution {    public boolean search(int[] A, int target) {        for (int i = 0; i < A.length; i ++) {            if (A[i] == target) {                return true;            }        }        return false;    }}



0 0
原创粉丝点击