【LeetCode OJ 136】Single Number

风格不统一 提交于 2019-12-26 02:13:06

题目链接:https://leetcode.com/problems/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?

解题思路:题意为:给定一个数组。仅仅有一个元素出现了一次。其他元素都出现了两次,找出那个仅仅出现一次的数。

能够遍历数组。分别进行异或运算。

注:异或运算:同样为0,不同为1。遍历并异或的结果就是那个仅仅出现了一次的数。

演示样例代码:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!