本文共 402 字,大约阅读时间需要 1 分钟。
题目:
Given an array of integers, every eleme:nt 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?
思路:给定一个数组,其中只有一个数是只出现 一次,其它都是出现两次,用按位异或来解,因为相同的两个数的异或肯定为0,最后只剩下了那个只出现 过一次的数。
代码:
class Solution {public: int singleNumber(vector & nums) { int result=0; for(int i=0;i
转载地址:http://jnmii.baihongyu.com/