Question:
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?
Analysis:
问题描述:给出一组数据,除只有一个数字出现1次外,每个数字出现两次。找出只出现一次的数字。
注意:你的算法应该在线性时间内运行,且不使用额外的空间。
Answer:
public class Solution { public int singleNumber(int[] nums) { if(nums == null || nums.length == 0) return 0; int result = 0; for(int i=0; i<nums.length; i++) { result = result ^ nums[i]; } return result; } }
时间: 2024-10-18 04:06:19