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?
题意
数列中有一个数出现了一次,其余数都出现了2次,找出这个出现一次的数
算法要求o(n)并且建议不使用多余内存空间
解题思路
神奇的异或世界
a^b^a = b(咳咳咳
AC代码
/**
class Solution {
public:
int singleNumber(int A[], int n) {
int tmp=0;
for(int i = 0 ; i < n; i++)
tmp^=A[i];
return tmp;
}
};
本文介绍LeetCode上Single Number题目的解题思路及实现方法。该题要求从一个整数数组中找出只出现一次的数,其余元素均出现两次。文章详细解释了如何利用异或操作达到线性时间复杂度且不使用额外空间的目标。
860

被折叠的 条评论
为什么被折叠?



