Leetcode ☞ 136. Single Number

https://leetcode.com/problems/single-number/


136. Single Number

My Submissions
Question
Total Accepted: 115703  Total Submissions: 237455  Difficulty: 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?






分析:逐个异或运算。因为 X ^ X = 0.


我的AC:

int singleNumber(int* nums, int numsSize) {
    int ans = 0;
    for (int i= 0; i < numsSize; i++){
         ans ^= *(nums + i);
    }
    return ans;
}

你可能感兴趣的:(Leetcode ☞ 136. Single Number)