首页 > 代码库 > [leetcode]Search in Rotated Sorted Array II
[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" 类似,只对返回值做少许修改即可。具体参见《Search in Rotated Sorted Array》
代码:
bool search(int A[], int n, int target) { //C++ if(n < 0) return false; if(n == 1) { if(A[0] == target) return true; else return false; } if(A[0] == target) return true; for(int i = 1; i < n; i++) { if(A[i] < A[i-1]) { if(A[i] > target || A[i-1] < target || target > A[n-1]) return false; } if(A[i] == target ) return true; } return false; }
[leetcode]Search in Rotated Sorted Array II
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。