easy?落單的數 查看執行結果?
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 hereint 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 herefor (auto x:A){int i = 0;for (auto y:A){if(x == y){++i;}}if (1==i){return x;}}}
};