540. Single Element in a Sorted Array

原创 2017年04月09日 13:08:31

原题

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

代码分析

异或操作求单数。

代码实现

        public int SingleNonDuplicate(int[] nums)
        {
            int rtn = nums[0];
            for(int i=1; i<nums.Length;i++)
            {
                rtn ^= nums[i];
            }
            return rtn;
        }
版权声明:本文为博主原创文章,欢迎转载,请注明 http://blog.csdn.net/daigualu

LeetCode 540. Single Element in a Sorted Array

题目: Given a sorted array consisting of only integers where every element appears twice except for o...

153. Find Minimum in Rotated Sorted Array\229. Majority Element II\Moore's voting algorithm

Find Minimum in Rotated Sorted Array 题目描述 代码实现153. Find Minimum in Rotated Sorted Array题目描述一个已经排好序的数...

Kth smallest element (in a sorted matrix ; In a BST tree ; In two sorted array)

三个问题的Java实现 1.Kth smallest element in a sorted matrix 2.Kth smallest element in a BST(Binary Search ...

27. Remove Element \ 66. Plus One \ 88. Merge Sorted Array

27. Remove Element法一:class Solution { public: int removeElement(vector& nums, int val) { ...

leetcode题解-26. Remove Duplicates from Sorted Array && 27. Remove Element

Remove Duplicates from Sorted Array,题目:Given a sorted array, remove the duplicates in place such tha...

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

题目: Given a sorted array, remove the duplicates in place such that each element appear only once an...

[leetcode] 378. Kth Smallest Element in a Sorted Matrix 解题报告

题目链接: https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/ Given a n x n matrix ...

LeetCode笔记:378. Kth Smallest Element in a Sorted Matrix

在行列分别升序的矩阵中找第 k 小的元素

378. Kth Smallest Element in a Sorted Matrix 解题报告

Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

LeetCode 378. Kth Smallest Element in a Sorted Matrix 解题报告

LeetCode 378. Kth Smallest Element in a Sorted Matrix 解题报告
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:540. Single Element in a Sorted Array
举报原因:
原因补充:

(最多只允许输入30个字)