Single Number

来源:互联网 发布:招警考试软件 编辑:程序博客网 时间:2024/06/08 06:52

136. Single Number

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?

给定一个整型数组,除了一个元素以外其余每个元素出现两次。找出那一个元素。
Note: 算法应在线性时间内完成,你能不使用额外的内存实现该算法吗

`    public class Solution {        public int singleNumber(int[] nums) {            int a = 1;            for (int i = 0; i < nums.length; i++) {                a ^= nums[i];            }             return a ^ 1;        }    }`

这道题我们用两个数异或,相同则为0,不同则保持不变的思想去做就好做。

0 0
原创粉丝点击