【从零单刷LeetCode】Single Number

没错我就是伍声2009的粉丝,从今天起,模仿《从零单排》系列,菜鸡单刷LeetCode!

题目:

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

解答:

其实就是找不同。这题需要一个比较trick的思路:位运算

试想,任何两个相同的数,进行异或操作答案是啥?是0. 4^8^4^8 = 0. 中间相隔N位也是如此。

0与任何数进行异或操作,答案是啥?是不是这个数自身。

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

【从零单刷LeetCode】Single Number的相关文章

【LeetCode从零单刷】Single Number II

题目: Given an array of integers, every element appears three times except for one. Find that single one. 解答: 如果是挑出非偶数次数的元素,可以利用每个元素依次异或的方法.但是非奇数次数(3,5,7,9--)的元素? 如果要快,可以hash_map存储.然后遍历一遍hash_map找到Single Number. 换种思路,按位找不同的Single Number对应的位.细节上: bit =

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 [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 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

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&mdash;&mdash;Single Number II(找出数组中只出现一次的数2)

问题: 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 I 升级版,一个数组中其它数出现了

LeetCode Single Number I II Python

Single Number Given an array of integers, every element appears twice except for one. Find that single one. def singleNumber(self, A): l = len(A) if l < 2: return A[0] A.sort() for i in range(0,l-1,2): if A[i] != A[i+1]: return A[i] return A[l-1] 思路: