0
点赞
收藏
分享

微信扫一扫

leetcode算法题--有序数组中的单一元素

朱悟能_9ad4 2022-02-20 阅读 49

原题链接:https://leetcode-cn.com/problems/single-element-in-a-sorted-array/

class Solution {
public:
    int singleNonDuplicate(vector<int>& nums) {
        int l = 0, h = nums.size() - 1;
        while (l < h) {
            int m = (h - l) / 2 + l;
            if (nums[m] == nums[m ^ 1]) {
                l = m + 1;
            } else {
                h = m;
            }
        }
        return nums[l];
    }
};
举报

相关推荐

0 条评论