[Leetcode]Single Number

来源:互联网 发布:苏州java招聘 编辑:程序博客网 时间:2024/05/18 00:02

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

思路:

1. 使用HASH表存储每个数组元素出现的次数

2. 理解按位异或操作的本质原理


int singleNumber(int* nums, int numsSize) {    int result = 0;    int i = 0;        for (i = 0; i < numsSize; i ++){                result ^= nums[i];    }        return result;}

采用位操作的方式比较精炼。

0 0
原创粉丝点击