Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
1.0 kB
4
Indexable
class Solution {
public:
    int singleNumber(vector<int>& nums) {
     
        const int N = 3 * 1e4 + 2;
        vector<int> postiveNum(N, 0), negativeNum(N, 0);

        for (int num : nums) {
            if (num < 0)
                negativeNum[num * -1]+=1;
            else
                postiveNum[num] += 1;
        }
        for (int i = 0; i < N; ++i) {
            if (negativeNum[i] == 1) {
                return i * -1;
            }
        }

        for (int i = 0; i < N; ++i) {
            if (postiveNum[i] == 1) {
                return i;
            }
        }
        return 0;
    }
};
__________________

class Solution
{
    public:
        int singleNumber(vector<int> &nums)
        {
            cin.tie(nullptr)->sync_with_stdio(0);
            int x {0};
            for (int i = 0; i < (int) nums.size(); ++i)
            {
                x ^= nums[i];
            }
            return x;
        }
};



















Leave a Comment