LeetCode之Single Number

66 阅读1分钟

1、题目

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?

 

Subscribe to see which companies asked this question.

2、代码实现

代码实现1

public static int singleNumber1(int nums[]) {
		if (nums == null) 
			return 0;
		int length = nums.length;
		int count = 0;
		for (int i = 0; i < length ; i++) {
			 for (int j = 0; j < length; j++) {
				  if (i != j) {
					  if (nums[i] == nums[j]) 
						  	continue;
					  else {
						    count++;  
					  }
				  }
			 }
			 if (count == length - 1)
				 return nums[i];
			 count = 0;
		}		
		return 0;