LinCode落单的数

寻找唯一不重复的数
在给定的数组中,除了一个数外,其他每个数都出现了两次。使用位操作解决此问题,实现一次遍历并常数级的额外空间复杂度。
容易 落单的数
60%
通过

给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字。


您在真实的面试中是否遇到过这个题? 
Yes
样例

给出 [1,2,2,1,3,4,3],返回 4

挑战

一次遍历,常数级的额外空间复杂度

位运算

class Solution {
public:
	/**
	 * @param A: Array of integers.
	 * return: The single number.
	 */
    int singleNumber(vector<int> &A) {
        // write your code here
        int a =0;
        for (auto x :A){
            a ^=x;
        }
        return a;
    }
};

暴力求解

class Solution {
public:
	/**
	 * @param A: Array of integers.
	 * return: The single number.
	 */
    int singleNumber(vector<int> &A) {
        // write your code here
        for (auto x:A){
            int i = 0;
             for (auto y:A){
                 if(x == y){
                     ++i;
                 }
             }
             if (1==i){
                 return x;
             }
        }
    }
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值