LeetCode 81. Search in Rotated Sorted Array II
来源:互联网 发布:mac怎么卸载 编辑:程序博客网 时间:2024/06/07 07:34
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.
Worst Time Complexity would be O(N).... for example: whole array is [4, 4, 4, 4, 4.....4] then the target is 3.
#include <vector>#include <iostream>using namespace std;bool search(vector<int>& nums, int target) { if(nums.size() == 0) return false; int low = 0; int high = nums.size() - 1; while(low <= high) { int mid = low + (high - low) / 2; if(nums[mid] == target) return true; if(nums[mid] < nums[high]) { if(target <= nums[high] && target > nums[mid]) { low = mid + 1; } else high = mid - 1; } else if(nums[mid] > nums[high]) { if(target >= nums[low] && target < nums[mid]) { high = mid - 1; } else { low = mid + 1; } } else { high--;} } return false;}// duplicates in rotated sorted array.// For example: 4, 4, 5, 6, 1, 2, 3, 4, target is 3.int main(void) { vector<int> nums{4, 4, 5, 6, 1, 2, 3, 4}; int target = 2; bool found = search(nums, target); cout << found << endl;}
0 0
- leetcode题解-33. Search in Rotated Sorted Array && 81. Search in Rotated Sorted Array II
- LeetCode 33. Search in Rotated Sorted Array && 81. Search in Rotated Sorted Array II
- LeetCode: Search in Rotated Sorted Array II
- LeetCode Search in Rotated Sorted Array II
- LeetCode : Search in Rotated Sorted Array II
- [LeetCode] Search in Rotated Sorted Array II
- [Leetcode] Search in Rotated Sorted Array II
- [leetcode] Search in Rotated Sorted Array II
- [LeetCode]Search in Rotated Sorted Array II
- [leetcode] Search in Rotated Sorted Array II
- LeetCode-Search in Rotated Sorted Array II
- LeetCode - Search in Rotated Sorted Array II
- 【Leetcode】Search in Rotated Sorted Array II
- Leetcode: Search in Rotated Sorted Array II
- 【leetcode】Search in Rotated Sorted Array II
- leetcode Search in Rotated Sorted Array II
- Leetcode: Search in Rotated Sorted Array II
- [LeetCode] Search in Rotated Sorted Array II
- 如何支持多种设备-语言篇
- QML Applications
- Objective-C 内存管理
- 数据结构算法——顺序表应用
- 基于Easyx的文本显示器的C语言实现——源码+实验报告
- LeetCode 81. Search in Rotated Sorted Array II
- 装饰器 ---- python
- LeetCode——049
- Qt QML
- 今日BBC-4/24
- qt quick与qt designer区别
- 树状数组
- jQuery 效果 - slideToggle() 方法
- QQmlApplicationEngine