【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的元素


class Solution {
public:
map<int,int> m;
int singleNumber(int A[], int n) {
for(int i = 0; i < n; i ++)
{
map<int,int>::iterator it = m.find(A[i]);
if(it == m.end())
m.insert(map<int,int>::value_type(A[i], 1));
else
it->second = 2;
}

for(map<int,int>::iterator it = m.begin(); it != m.end(); it ++)
{
if(it->second == 1)
return it->first;
}
}
};

解法二:利用异或操作的结合律。同一数字异或自己结果为0.x^x=0


class Solution
{
public:

int singleNumber(int A[], int n)
{
int sum = 0;
for(int i = 0; i < n; i ++)
sum ^= A[i];
return sum;
}
};

【LeetCode】Single Number (2 solutions),布布扣,bubuko.com

时间: 2025-01-04 02:22:50

【LeetCode】Single Number (2 solutions)的相关文章

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

题意: 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 分别记录数字在二

【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)的方法,都超时,后来

【LeetCode】Reverse Integer (2 solutions)

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alread

【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