LinCode落单的数
生活随笔
收集整理的這篇文章主要介紹了
LinCode落单的数
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
easy?落單的數(shù) 查看執(zhí)行結(jié)果?
60% 通過給出2*n + 1?個的數(shù)字,除當(dāng)中一個數(shù)字之外其它每一個數(shù)字均出現(xiàn)兩次。找到這個數(shù)字。
給出?[1,2,2,1,3,4,3]。返回 4
挑戰(zhàn)一次遍歷,常數(shù)級的額外空間復(fù)雜度
位運(yùn)算
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;}}} };
轉(zhuǎn)載于:https://www.cnblogs.com/lxjshuju/p/6769111.html
總結(jié)
以上是生活随笔為你收集整理的LinCode落单的数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Examplematcher 模糊查询需
- 下一篇: CF-798B