Single Number

来源:互联网 发布:拼多多比淘宝好做吗 编辑:程序博客网 时间:2024/05/16 17:28

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 Solution {    public int singleNumber(int[] A) {        int n=A[0];        for(int i=1;i<A.length;i++){            n^=A[i];        }        return n;    }}
主要运用到了异或运算的性质。

0 0
原创粉丝点击