LeetCode | Single Number

来源:互联网 发布:wind数据付费版多少钱 编辑:程序博客网 时间:2024/05/16 19:50

原题描述:https://oj.leetcode.com/problems/single-number/

Given an array of integers, every element appears twice except for one. Find that single one.


解题思路

可以利用异或运算来实现。关于异或运算的几个法则:

a ^ a = 0;

a ^ 0 = a;

a ^ b = b ^ a ; 


实现代码

public class Solution {public int singleNumber(int[] A) {int result = 0;for (int i : A) {result ^= i;}return result;}}


0 0
原创粉丝点击