Single Number

来源:互联网 发布:怎么提高淘宝店铺权重 编辑:程序博客网 时间:2024/06/03 18:39

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?


//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 static int singleNumber(int[] A) {int num = A[0];for (int i = 1; i < A.length; i++) {num ^= A[i];}return num;}public static void main(String[] args) {int[] a = {1,3,4,1,2,4,3,2,9};int num = SingleNumber.singleNumber(a);System.out.println(num);}}

Single Number

Submission Details

15 / 15 test cases passed.
Status: 

Accepted

Runtime: 420 ms
Submitted: 0 minutes ago

0 0