leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0081.cpp (692B)


      1 class Solution {
      2   public:
      3     bool search(const vector<int> &nums, int target) {
      4         int left = 0, right = nums.size() - 1;
      5         while (left <= right) {
      6             int mid = left + (right - left) / 2;
      7             if (nums[mid] == target) return true;
      8             if (nums[mid] == nums[left]) {
      9                 left++;
     10                 continue;
     11             }
     12 
     13             bool pivotArr = nums[left] <= nums[mid];
     14             bool targetArr = nums[left] <= target;
     15             if ((pivotArr ^ targetArr) ? (pivotArr) : (nums[mid] < target)) {
     16                 left = mid + 1;
     17             } else {
     18                 right = mid - 1;
     19             }
     20         }
     21 
     22         return false;
     23     }
     24 };