LeetCode-Easy刷题(31) Single Number

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sqh201030412/article/details/78679841

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?


给定一个数组中除了其中一个元素外其余的都出现两次,找出它.
要求线性复杂,不使用额外空间.


//位运算 亦或
    public int singleNumber(int[] nums) {

        int result = nums[0];

        for (int i = 1; i < nums.length; i++) {
            result = result^nums[i];
        }
        return result;
    }




猜你喜欢

转载自blog.csdn.net/sqh201030412/article/details/78679841