题目:
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?
这题目的要求不仅是要求是线性时间,希望也不会使用额外的内存,那么也就是你无法运用其他的数据结构。也是参考了其他人的答案。对于位操作的特性还有待进一步挖掘。
class Solution { public: int singleNumber(vector<int>& nums) { int num = 0; for (int i = 0; i < nums.size(); ++i){ num ^= nums[i]; } return num; } };
时间: 2024-10-26 00:29:52