136. 只出现一次的数字 + 异或

136. 只出现一次的数字

LeetCode_136

题目描述

相似题目:剑指 Offer 56 - I. 数组中数字出现的次数

代码实现

class Solution {
    public int singleNumber(int[] nums) {
        int n = nums.length;
        int ans = 0;
        for(int i=0; i<n; i++){
            ans ^= nums[i];
        }
        return ans;
    }
}
posted @ 2021-03-31 20:17  Garrett_Wale  阅读(49)  评论(0编辑  收藏  举报