【LeetCode】Single Number

来源:互联网 发布:软件的收费模式 编辑:程序博客网 时间:2024/05/29 11:45

【题目】

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?

【分析】

方法一:bit Manipulation!!

使用异或

同一变量与另一变量和其异或值异或等于自身。

a^a=0

B=a^a^B

所以随后的值就是单独留下的那一个咯!~很聪明的做法咧~

【代码】

public static int Single(int[] A){for(int i=1;i<A.length;i++){A[0]^=A[i];System.out.println(i+":"+A[0]);}return A[0];}




0 0
原创粉丝点击