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,3,7,7,10,11,11]
Output: 10

Note: Your solution should run in O(log n) time and O(1) space.

解题思路:

要求我们要在O(logn)时间和O(1)空间内解决,首先想到二分搜索。

在二分搜索的应用中,如何找到移动的条件是十分关键的。

我们先观察一下:

值:  1 1 2 2 4 4 5 5 9

下标: 0 1 2 3 4 5 6 7 8

此时中值为4,下标也为4。

若从头到尾都是成双成对,那偶数位的应该等于它后面的奇数位。

如果不等于,那么说明前半段出现了一个奇数。

若中值位一个奇数,若它与前面的数字相同,则说明前面都是成双成对,只有一个的数字是出现在后面。

同时我们也需要判断当前值是否是我们要找的值

即不等于左边的值,又不等于右边的值

if((nums[mid] != l_Num || mid == 0) && (nums[mid] != r_Num || mid == n-1)){
                return nums[mid];
            }

在这里需要注意的是数组头部和尾部的判断,否则会出现死循环

代码:

class Solution {
public:
    int singleNonDuplicate(vector<int>& nums) {
        int n = nums.size();
        if(n == 1)
            return nums[0];
        int left = 0;
        int right = n-1;
        int mid;
        while(left <= right){
            mid = left + (right - left)/2;
            int l_Num = mid-1 > -1 ? nums[mid-1] : nums[mid];
            int r_Num = mid + 1 < n ? nums[mid+1] : nums[mid];
            if((nums[mid] != l_Num || mid == 0) && (nums[mid] != r_Num || mid == n-1)){
                return nums[mid];
            }
            if(mid % 2 == 1){
                if(l_Num == nums[mid]){
                    left = mid+1;
                }else{
                    right = mid;
                }
            }else{
                if(l_Num == nums[mid]){
                    right = mid;
                }else{
                    left = mid+1;
                }
            }
        }
        return nums[mid];
    }
};

原文地址:https://www.cnblogs.com/yaoyudadudu/p/9227155.html

时间: 2024-11-10 04:23:09

540. Single Element in a Sorted Array的相关文章

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,

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

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

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 1150. Check If a Number Is Majority Element in a Sorted Array

原题链接在这里:https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/ 题目: Given an array nums sorted in non-decreasing order, and a number target, return True if and only if target is a majority element. A majority element is

LeetCode:Remove Duplicates from Sorted Array &amp;&amp; Remove Element

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 =

LeetCode26/27/80 Remove Duplicates from Sorted Array I and II/Remove Element**

一: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 m

Searching an Element in a Rotated Sorted Array

Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). How do you find an element in the rotated array efficiently? You may assume no duplicate exists in the array. Note:I have upd

[LeetCode OJ] Single Number之一 ——Given an array of integers, every element appears twice except for one. Find that single one.

1 class Solution { 2 public: 3 int singleNumber(int A[], int n) { 4 int i,j; 5 for(i=0; i<n; i++) 6 { 7 for(j=i+1; j<n; j++) 8 { 9 if(A[j]==A[i]) 10 { 11 int temp = A[i+1]; 12 A[i+1] = A[j]; 13 A[j] = temp; 14 i++; 15 break; 16 } 17 } 18 if(j==n) 19