Leetcode#81Search in Rotated Sorted Array II

Search in Rotated Sorted Array II

Total Accepted: 35100 Total Submissions: 111308My Submissions

Question Solution

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.

Show Tags

分析:二分搜索法

public class Solution {

boolean findnum(int[] nums, int target, int s, int e){

if(s==e)

{

if(nums[s]==target)

return true;

else

return false;

}

else

{

if(nums[s]<nums[e])

{

if(nums[s]>target||target>nums[e])

return false;

else

{

int mid=s+(e-s)/2;

if(target==nums[mid])

return true;

else if(target<nums[mid])

return findnum(nums, target, s, mid);

else

return findnum(nums, target, mid+1,e);

}

}

else

{

if(target==nums[e]||target==nums[s])

return true;

else if(target>nums[e]&&target<nums[s])

{

return false;

}

else

{

int mid=s+(e-s)/2;

if(target==nums[mid])

return true;

else

{

boolean l=findnum(nums, target, s, mid);

boolean r=findnum(nums, target, mid+1, e);

if(l||r)

return true;

else

return false;

}

}

}

}

}

public boolean search(int[] nums, int target) {

int size=nums.length;

if(size==0)

return false;

else

{

return findnum(nums, target, 0, size-1);

}

}

}

时间: 2024-10-14 11:40:06

Leetcode#81Search in Rotated Sorted Array II的相关文章

[leetcode]Search in Rotated Sorted Array II @ Python

原题地址: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

[LeetCode] Search in Rotated Sorted Array II [36]

题目 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. 原题链接(点我) 解题思路 这题和Search in Rotated Sorted

LeetCode——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. 原题链接:https://oj.leetcode.com/problems/search

LeetCode: Search in Rotated Sorted Array II [081]

[题目] 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. [题意] 在"Search in Rotated Sorted Ar

LeetCode: Search in Rotated Sorted Array II 解题报告

Search in Rotated Sorted Array II Follow up for "LeetCode: 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 arr

leetcode:Search in Rotated Sorted Array II(duplicated)

题目要求: 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. 题目地址:https://leetcode.com/problems/sear

[leetcode]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. "Search in Rotated Sorted Array&q

[LeetCode] 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. Hide Tags Array Binary Search 这个是在 前一道的基础上改过来

[LeetCode] Find Minimumin Rotated Sorted Array II

题意:在旋转过的有序数组中找到最小数,数组中可能有重复元素 思路:二分,判断是否有相等的元素,主要是二分的时候的一些细节,比如说是有序的还是rotated 代码: public int findMin(List<Integer> nums) {//solution1 O(log(N)) int min = Integer.MAX_VALUE; int l = 0, r = nums.size()-1,mid; while (l < r){ if(l + 1 == r){ min = Ma