Leetcode 81. Search in Rotated Sorted Array II (Medium) (cpp)
2016-07-12 11:58
393 查看
Leetcode 81. Search in Rotated Sorted Array II (Medium) (cpp)
Tag: Array, Binary Search
Difficulty: Medium
Tag: Array, Binary Search
Difficulty: Medium
/* 81. Search in Rotated Sorted Array II (Medium) 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. */ class Solution { public: bool search(vector<int>& nums, int target) { int first = 0, last = nums.size() - 1; while (first != last) { int mid = first + (last - first) / 2; if (nums[mid] == target) { return true; } else if (nums[first] < nums[mid]) { if (nums[first] <= target && target < nums[mid]) { last = mid - 1; } else { first = mid + 1; } } else if (nums[first] > nums[mid]) { if (nums[mid] < target && target <= nums[last]) { first = mid + 1; } else { last = mid -1; } } else { first++; } } if (nums[first] == target) { return true; } return false; } };
相关文章推荐
- Leetcode 219. Contains Duplicate II (Easy) (cpp)
- Leetcode 217. Contains Duplicate (Easy) (cpp)
- Leetcode 189. Rotate Array (Easy) (cpp)
- Leetcode 88. Merge Sorted Array (Easy) (cpp)
- Leetcode 33. Search in Rotated Sorted Array (Hard) (cpp)
- Leetcode 80. Remove Duplicates from Sorted Array II (Medium) (cpp)
- C++中的单例模式(懒汉、饿汉)
- BJOI2011 严格次小生成树 解题报告
- C++软件工程师,你该会什么?
- C++软件工程师,你该会什么?
- Leetcode 205. Isomorphic Strings (Easy) (cpp)
- Leetcode 118. Pascal's Triangle (Easy) (cpp)
- Leetcode 119. Pascal's Triangle II (Easy) (cpp)
- Leetcode 27. Remove Element (Easy) (cpp)
- C++11多线程系列第一部分
- C++ split 分割字符串
- C++ #pragma 用法小结
- Leetcode 26. Remove Duplicates from Sorted Array (Easy) (cpp)
- JNI使用实例之C、C++ DLL回调java成员函数
- 串算法 - 蛮力匹配 Brute Force