[LeetCode] 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),使得不能用排序方法,也不能使用map数据结构。那么只能另辟蹊径,这个解法如果让我想,肯定想不出来,因为谁会想到用逻辑异或来解题呢。逻辑异或的真值表为:

异或运算真值表如下:

A B
F F F
F T T
T F T
T T F

由于数字在计算机是以二进制存储的,每位上都是0或1,如果我们把两个相同的数字异或,0与0异或是0,1与1异或也是0,那么我们会得到0。根据这个特点,我们把数组中所有的数字都异或起来,则每对相同的数字都会得0,然后最后剩下来的数字就是那个只有1次的数字。这个方法确实很赞,但是感觉一般人不会忘异或上想,绝对是为CS专业的同学设计的好题呀,赞一个~~

代码如下:

class Solution {
public:
    int singleNumber(int A[], int n) {
        int res = A[0];
        for (int i = 1; i < n; ++i) res ^= A[i];
        return res;
    }
};
时间: 2024-11-03 08:52:11

[LeetCode] Single Number 单独的数字的相关文章

[LintCode] Single Number 单独的数字

Given 2*n + 1 numbers, every numbers occurs twice except one, find it. Have you met this question in a real interview? Yes Example Given [1,2,2,1,3,4,3], return 4 Challenge One-pass, constant extra space. LeetCode上的原题,请参见我之前的博客Single Number. class So

[LeetCode] Missing Number 丢失的数字

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. For example, Given nums = [0, 1, 3] return 2. Note: Your algorithm should run in linear runtime complexity. Could you implement it u

leetCode: Single Number II [137]

[题目] 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? [题意] 给定一个整数以外,其中除了一个整数只出现一次以外,其他

LeetCode: Single Number Ⅱ

1 /** 2 * 3 */ 4 package solution; 5 6 import java.util.Arrays; 7 8 /** 9 * @author whh 10 * 11 * Given an array of integers, every element appears three times except 12 * for one. Find that single one. 13 * 14 * Note: Your algorithm should have a li

LeetCode: Single Number [136]

[题目] 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? [题意] 给定一个整数数组,其中除了一个数以外,其他数都是成对出现的,找出这

LeetCode——Single Number(II)

Single Number Given an array of integers, every element appears twice except for one. Find that single one. Single Number II Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm shou

[LeetCode] 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? 这道题是之前那道单独的数字的延伸,那道题的解法就比较独特,是利用计算机按位储

LeetCode Single Number I / II / III

[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个相同的数异或为0,那么把这串数从头到尾异或起来,最后的数就是要求的那个数. 代码如下: class Solution { public: int singleNumber(vector<int>& nums) { int sum = 0; for(int i=0;i<nums.siz

leetcode Single Number III

题目连接 https://leetcode.com/problems/single-number-iii/ Single Number III Description 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