single_non_duplicate

  • 2022-12-14
  • 浏览 (499)

single_non_duplicate.cpp 源码

// 有序数组中的单一元素

#include "iostream"
#include "vector"
using namespace std;

class Solution {
public:
    int singleNonDuplicate(vector<int>& nums) {
        int low = 0, high = nums.size() - 1;
        while (low < high) {
            int mid = low + (high - low) / 2;
            if (mid % 2 == 1) {
                mid--;
            }
            if (nums[mid] == nums[mid + 1]) {
                low = mid + 2;
            } else {
                high = mid;
            }
        }
        return nums[low];
    }
};

你可能感兴趣的文章

find_min_rotated_array

first_bad_version

first_last_in_array

0  赞