forked from lilong-dream/LeetCode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path136SingleNumber.java
More file actions
28 lines (23 loc) · 937 Bytes
/
136SingleNumber.java
File metadata and controls
28 lines (23 loc) · 937 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Author: Li Long, [email protected]
// Date: Apr 17, 2014
// Source: http://oj.leetcode.com/problems/single-number/
// Analysis: http://blog.csdn.net/lilong_dream/article/details/19556493
// 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?
public class SingleNumber {
public int singleNumber(int[] A) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int result = 0;
for(int num : A) {
result ^= num;
}
return result;
}
public static void main(String[] args) {
SingleNumber slt = new SingleNumber();
int[] A = new int[] { 1, 2, 1, 2, 3, 4, 4 };
System.out.println(slt.singleNumber(A));
}
}