Skip to content

LeetCode: 只出现一次的数字 #91

Description

@resse92

这题说起来不难,但是要求线性时间复杂度以及不能使用额外的空间,所以只能依赖于异或的特性:相同的数字异或等于0。

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int num = 0;
        for (int i = 0; i < nums.size(); i++) {
            num = num ^ nums[i];
        }
        return num;
    }
};

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions