LeetCode-Single Number-解题报告

来源:互联网 发布:乐天 韩国 日本 知乎 编辑:程序博客网 时间:2024/06/05 20:27

原题链接 https://leetcode.com/problems/single-number/

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

找出唯一一个只出现一次的数。

因为其他数肯定出现两次 又因为A^A = 0, 0^B = B;

所以将数组中所有的数全部异或起来就找到了唯一出现的数

class Solution {public:int singleNumber(vector<int>& nums) {int res = 0;for (auto& it : nums)res ^= it;return res;}};


0 0
原创粉丝点击