Leetcode 位运算 Single Number

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie

Single Number

Total Accepted: 20063 Total
Submissions: 44658

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?

题意:在一组数组中除一个元素外其它元素都出现两次,找出这个元素

思路:位运算。异或。因为异或操作可以交换元素的顺序,所以元素异或的顺序没影响,

最后出现再次的元素都会被异或掉,相当于0和只出现一次的那个元素异或,结果还是那个元素

推广:这个方法也适合于出现其它元素都出现偶数次,而要找的元素出现奇数次的情况

复杂度:时间O(n),空间O(1)

相关题目:Single Number II

class Solution {
public:
    int singleNumber(int A[], int n) {
        int temp = 0;
        for(int i = 0; i < n; i++){
            temp = temp ^ A[i];
        }
        return temp;
    }
};

Leetcode 位运算 Single Number

时间: 2024-10-19 19:36:17

Leetcode 位运算 Single Number的相关文章

Leetcode 位运算 Single NumberII

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Single Number II Total Accepted: 14224 Total Submissions: 43648 Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have

【一天一道LeetCode】#260. Single Number III

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find >the two elements that a

LeetCode OJ 之 Single Number III (唯一的数字-三)

题目: Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. For example: Given nums = [1, 2, 1, 3, 2, 5], return [3, 5]. Note: The or

LeetCode 笔记26 Single Number II

Given an array of integers, every element appears three times 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 si

leetcode笔记:Single Number II

一.题目描述 二.解题思路 这道题与Single Number(数组中其他数出现两次,仅有一个出现一次的)有所不同,本题变为序列中有一个数出现一次,其他元素出现了三次,同样要求时间复杂度为线性,空间复杂度为常数.事实上,该算法仍可以借助位运算来实现. 首先需要确定int类型数据的长度:intWidth = sizeof(int) * 8,可以用intWidth大小的变量来存储数组中每个元素的各个二进制位上1 出现的次数,最后 在进行 模3 操作,如果为1,那说明这一位是要找元素二进制表示中为 1

BitMap - leetcode [位运算]

136. Single Number 因为A XOR A = 0,且XOR运算是可交换的,于是,对于实例{2,1,4,5,2,4,1}就会有这样的结果: (2^1^4^5^2^4^1) => ((2^2)^(1^1)^(4^4)^(5)) => (0^0^0^5) => 5异或:异为1 137. Single Number II(黑人问号脸) 对于除出现一次之外的所有的整数,其二进制表示中每一位1出现的次数是3的整数倍,将所有这些1清零,剩下的就是最终的数.用ones记录到当前计算的变量

LeetCode Problem 136: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? 题目要求O(n)时间复杂度,O(1)空间复杂度. 思路1:初步使用暴力搜索,遍历

【LeetCode】-- 260. Single Number III

问题描述: https://leetcode.com/problems/single-number-iii/ 在一个数组里面,只有两个元素仅出现过1次,其余都出现过两次.找出出现仅一次的那两个(a, b). 要求常量空间,线性时间. 问题解决: 这题用到“神奇的位运算”. 1.因为除了特殊的两个元素,其余两两出现,那么就想到了XOR(异或). 2.从头到尾XOR之后,会得到a xor b 的结果.接下来我们试着把这两个元素分离开来. 3.我们在a xor b 中找到任意一位XOR[diff_po

LeetCode 137:Single Number II

Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? Single Number II 比Single Number要复杂的多,