现在的位置: 首页 > 综合 > 正文

【LeetCode题解】Single Number

2018年04月04日 ⁄ 综合 ⁄ 共 361字 ⁄ 字号 评论关闭

Single Number(Java代码)

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[] A) {
		int number = 0;
		for (int i = 0; i < A.length; i++) {
			number = number ^ A[i];
		}
		return number;
	}
}

抱歉!评论已关闭.