540 Single Element in a Sorted Array 有序数组中的单一元素

给定一个只包含整数的有序数组,每个元素都会出现两次,唯有一个数只会出现一次,找出这个数。
示例 1:
输入: [1,1,2,3,3,4,4,8,8]
输出: 2

示例 2:
输入: [3,3,7,7,10,11,11]
输出: 10
注意: 您的方案应该在 O(log n)时间复杂度和 O(1)空间复杂度中运行。
详见:https://leetcode.com/problems/single-element-in-a-sorted-array/description/

C++:

class Solution {
public:
    int singleNonDuplicate(vector<int>& nums) {
        int res=0;
        for(int num:nums)
        {
            res^=num;
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/xidian2014/p/8910325.html

时间: 2024-11-05 12:21:22

540 Single Element in a Sorted Array 有序数组中的单一元素的相关文章

Leetcode 540.有序数组中的单一元素

有序数组中的单一元素 给定一个只包含整数的有序数组,每个元素都会出现两次,唯有一个数只会出现一次,找出这个数. 示例 1: 输入: [1,1,2,3,3,4,4,8,8] 输出: 2 示例 2: 输入: [3,3,7,7,10,11,11] 输出: 10 注意: 您的方案应该在 O(log n)时间复杂度和 O(1)空间复杂度中运行. 思路 取中间组坐标:mid = ( left + right ) / 2若 mid组中两元素相同 则唯一出现一次的元素必在 mid+1 组 到 right 组中(

[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example,Given input array A = [

540. Single Element in a Sorted Array(LeetCode)

Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once. Example 1: Input: [1,1,2,3,3,4,4,8,8] Output: 2 Example 2: Input: [3,3,7,7,

540. Single Element in a Sorted Array

问题描述: Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once. Example 1: Input: [1,1,2,3,3,4,4,8,8] Output: 2 Example 2: Input: [3,

LeetCode 第540题 有序数组中的单一元素

/*给定一个只包含整数的有序数组,每个元素都会出现两次,唯有一个数只会出现一次,找出这个数.[0,1,1,2,2,5,5] 示例 1: 输入: [1,1,2,3,3,4,4,8,8] 输出: 2 示例 2: 输入: [3,3,7,7,10,11,11] 输出: 10 注意: 您的方案应该在 O(log n)时间复杂度和 O(1)空间复杂度中运行.*/ /* 思路: 用二分法.mid 为偶数: mid和哪边元素相同,单个的数就在哪边.mid 为奇数: mid和哪边元素不同,单个的数就在哪边.为了简

LeetCode.540.Single Element in a Sorted Array

恩, 沙比提 1 class Solution(object): 2 def singleNonDuplicate(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: int 6 """ 7 xorsum = 0 8 for i in nums: 9 xorsum = xorsum ^ i 10 return xorsum 11

Single Element in a Sorted Array

Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once. Example 1: Input: [1,1,2,3,3,4,4,8,8] Output: 2 Example 2: Input: [3,3,7,7,

leetCode 88. Merge Sorted Array 有序数组

88. Merge Sorted Array Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The n

[LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. Note that it is the kth smallest element in the sorted order, not the kth distinct element. Example: matrix = [ [ 1, 5