关闭

540. Single Element in a Sorted Array

标签: leetcode位操作
708人阅读 评论(5) 收藏 举报
分类:

原题

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;
        }
2
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    Leetcode Solutions in Github

    Click me

    Leetcode solutions in Github, including tags: Array, HashTable, Stack, Queue, LinkedList, Tree, Graph,...

    leetcode-manager

    Click me

    Current Leetcode manager: it's used by Entity Framework, WinForms, Sql Server to manage leetcode questions that we have done. Functions include tags creation, revise and deletion, and questions creation, edit and deletion.

    个人资料
    • 访问:126185次
    • 积分:5037
    • 等级:
    • 排名:第5927名
    • 原创:311篇
    • 转载:0篇
    • 译文:0篇
    • 评论:53条
    博客专栏