[Leetcode] Single Number

    xiaoxiao2023-03-25  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?

    利用两个相同的数异或等于0,还有异或交换律。

    public class Solution { public int singleNumber(int[] nums) { int num=0; int len=nums.length; while(len>0) {num=num^nums[len-1]; len--; } return num; } }

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