【leetcode】Single Number (Medium) ☆

题目:

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(n^2)的方法,都超时,后来突然顿悟了。异或可以消掉两个相同数字。

直接把所有数字异或在一起,就是单独的那个数字了。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Solution {
public://异或可以把两个相同的数字消除 O(n) AC
    int singleNumber3(int A[], int n) {
        int ans = 0;
        for(int i = 0; i < n; i++)
        {
            ans ^= A[i];
        }
        return ans;
    }
};

int main()
{
    Solution s;
    int a[9] = {0,0,1,1,2,3,2,3,4};
    int n = s.singleNumber3(a, 9);

    return 0;
}
时间: 2024-10-11 12:47:36

【leetcode】Single Number (Medium) ☆的相关文章

【LeetCode】Single Number (2 solutions)

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? 解法一:用map记录每个元素的次数,返回次数为1的元素 cl

【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? 解答: 常规解法:先对数组进行排序,然后通过按顺序判断每相邻两个数是否相同即可

【LeetCode】Single Number I &amp; II

Single Number I : 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? Solution: 解法不少,贴一种: 1 cla

【LeetCode】Single Number II (3 solutions)

Single Number II Given an array of integers, every element appears threetimes except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解法一:开辟map记录次数 class So

【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? 思路: 用 ones, twos, threes 分别记录数字在二

【leetcode78】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 m

【LeetCode】Largest Number 解题报告

[题目] Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to return a string instead of

【LeetCode】Valid Number 解题报告

[题目] Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true Note: It is intended for the problem statement to be ambig

【从零单刷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与任何数进行异或操作,答案是啥?是不是