136. Single Number

    xiaoxiao2026-08-14  5

    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?

    a ^ b = b ^ a;

    0 ^ a = a;

    public int singleNumber(int[] nums) { if (nums == null || nums.length == 0) return 0; int res = nums[0]; for (int i = 1; i < nums.length; i++) { res ^= nums[i]; } return res; }

    转载请注明原文地址: https://ju.6miu.com/read-1311158.html
    最新回复(0)