标签:span ott 解法 off scribe nbsp color item bre
class Solution { public: void FindNumsAppearOnce(vector<int> data, int *num1, int *num2) { map<int, int> mapping; for (int i = 0; i < data.size(); i++) mapping[data[i]]++; vector<int> v; for (int i = 0; i < data.size(); i++) { if (mapping[data[i]] == 1) v.push_back(data[i]); } *num1 = v[0]; *num2 = v[1]; } };
解法二:
利用异或
class Solution { public: void FindNumsAppearOnce(vector<int> data, int *num1, int *num2) { int tempbit = 0; for (auto c : data) tempbit ^= c; int sign = 0; //记录两个数不同的那一位 for (; sign < data.size(); sign++) { if ((tempbit & (1 << sign)) != 0) break; } *num1 = 0; *num2 = 0; for (auto c : data) { if ((c & (1 << sign)) == 0) *num1 ^= c; else *num2 ^= c; } } };
标签:span ott 解法 off scribe nbsp color item bre
原文地址:https://www.cnblogs.com/ruoh3kou/p/10153048.html