LeetCode-Easy刷题(31) Single Number

Posted 当以乐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode-Easy刷题(31) Single Number相关的知识,希望对你有一定的参考价值。

Given an array of integers, every element appears twice except for one. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?


给定一个数组中除了其中一个元素外其余的都出现两次,找出它. 要求线性复杂,不使用额外空间.


//位运算 亦或
    public int singleNumber(int[] nums) 

        int result = nums[0];

        for (int i = 1; i < nums.length; i++) 
            result = result^nums[i];
        
        return result;
    




以上是关于LeetCode-Easy刷题(31) Single Number的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode-Easy刷题 Remove Element

LeetCode-Easy刷题(19) Same Tree

LeetCode-Easy刷题(33) Min Stack

LeetCode-Easy刷题(33) Min Stack

LeetCode-Easy刷题(26) Path Sum

LeetCode-Easy刷题 Implement strStr()