Leetcode 136. Single Number

    xiaoxiao2021-03-26  18

    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?

    s思路: 1. 这道题遇到过! 不用内存,那就是一直做xor运算,相同的数xor等于0,最后只剩下the single number. 2. 这道题就是利用两个数异或的性质,基本也就是一道数学题!

    class Solution { public: int singleNumber(vector<int>& nums) { int res=0; for(int i=0;i<nums.size();i++){ res^=nums[i]; } return res; } };
    转载请注明原文地址: https://ju.6miu.com/read-660294.html

    最新回复(0)