题解 | #缺失数字#(异或)

缺失数字

http://www.nowcoder.com/practice/9ce534c8132b4e189fd3130519420cde

异或 时间复杂度O(n)

class Solution {
public:
    int solve(vector<int>& a) {
        int n = a.size();
        if(n == 0) return 0;
        int result = 0;
        for(int i = 0; i < n; i++){
            result ^= a[i];
            result ^= i+1;
        }
        return result;
    }
};
全部评论

相关推荐

程序员饺子:正常 我沟通了200多个 15个要简历 面试2个 全投的成都的小厂。很多看我是27直接不会了😅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务