IT討論區(116) 加拿大救生艇門已開

狼王托迪10

1001 回覆
0 Like 1 Dislike
Code4Food 2021-04-20 01:58:22
補充小小。下面係四個有關XOR (^ operator in C/C++) 嘅property

1. a ^ a = 0
2. a ^ 0 = a
3. a ^ b = b ^ a // commutative
4. (a ^ b) ^ c = a ^ (b ^ c) // associative

3 同 4 加埋保證XOR一推數時,次序唔會應響結果。例如
1 ^ 2 ^ 3 ^ 4 = 4 ^ 3 ^ 2 ^ 1 = 1 ^ 4 ^ 2 ^ 3 = 3 ^ 1 ^ 4 ^ 2

如果一堆數目除咗一個單丁外其他係重複,例如:

1,2,3,4,1,3,4

XOR 晒:
1 ^ 2 ^ 3 ^ 4 ^ 1 ^ 3 ^ 4

= 1 ^ 1 ^ 2 ^ 3 ^ 3 ^ 4 ^ 4 // 3. + 4. order does not matter
= (1^ 1) ^ 2 ^ (3 ^ 3) ^ (4 ^ 4)
= 0 ^ 2 ^ 0 ^ 0 // 1. a ^ a = 0
= 2 // 2. a ^ 0 = a
Code4Food 2021-04-20 02:06:47
It is extremely inefficient to convert an int to string and then check the digits in string. You can find size of the largest consecutive group of zeroes by shifting and counting.
海獅牌床褥 2021-04-20 02:47:14
海獅牌床褥 2021-04-20 02:55:38
鳩彈彈 2021-04-20 03:05:46
startup 人工好少,通常要共渡患難,介唔介意講幾多人工
H07252 2021-04-20 04:20:19
狼王托迪10 2021-04-20 06:51:48
狼王托迪10 2021-04-20 06:53:12
Mikey_Williams 2021-04-20 07:58:16
巴打平時去咩網站操sql
唔計leetcode個啲
想要啲by topic 順住操嘅題目
膠の呼吸 2021-04-20 07:59:07
實驗羊 2021-04-20 07:59:58
係 呢招第一次睇真係串完之後發現自己都唔係好適合玩 Algo
實驗羊 2021-04-20 08:01:19
直接拎個 db 落嚟寫
狼王托迪10 2021-04-20 08:14:57
手一黏便緊(UTC+9 2021-04-20 08:20:11
諗左個變體
Given 4n+3 numbers, all numbers repeat exactly 4 times, except that A appear exactly once and B appear exactly twice
Find A and B

本來想諗啲難啲既變體 但係我自己都未solve到
Code4Food 2021-04-20 08:31:40
piece of cake. Solvable in O(n) time and O(1) space. XOR to find A. Then count the number of bits in each bit positions [0,1,...63], assuming sizeof(int) == 8. A bit is set in B iff count is 4n + 2. You can do that in one pass over the data.
Code4Food 2021-04-20 08:33:05
Actually, you can skip the XOR and just count the bits in each bit position. A bit is set in A iff count is 4n + 1. bit is set in B iff count is 4n + 2 or 4n + 3.
Code4Food 2021-04-20 08:53:13
Since we only care if number of bits modulo 4, bit counting can be done in parallel:

std::vector<int64> a; 

// count[i] store bit count of positions i % 4
// actually 3 is sufficient but 4 is easy to code.
int64 count[4] = {0};

for (int64 a_i : a) {
  // accumulate bit count % 4
  constexpr int64 mask_01 = 0x1111111111111111;
  constexpr int64 mask_11 = 0x3333333333333333;
  for (int i = 0; i < 4; i++) {
    const int64 bits = (a_i >> i) & mask_01;
    count[i] = (count[i] + bits) & mask_11;
  }
}

int64 a = 0, b = 0;
for (int i = 0; i < 4; i++) {
  a |= (count[i] & mask_01) << i;
  b |= ((count[i] >> 1) & mask_01) << i;
}
媽咪 2021-04-20 08:57:09
Trinidad 2021-04-20 09:14:29
讀書嘅時候又到
Mike_Chan 2021-04-20 09:16:45
themida 2021-04-20 09:20:39
幾乎所有lang都有
btw 技能點完全冇點落algo度
海獅牌床褥 2021-04-20 10:12:30
唔錯的 高過fg均價
海獅牌床褥 2021-04-20 10:13:46
狼王托迪10 2021-04-20 10:20:36
吹水台自選台熱 門最 新手機台時事台政事台World體育台娛樂台動漫台Apps台遊戲台影視台講故台健康台感情台家庭台潮流台美容台上班台財經台房屋台飲食台旅遊台學術台校園台汽車台音樂台創意台硬件台電器台攝影台玩具台寵物台軟件台活動台電訊台直播台站務台黑 洞