Re: [閒聊] 每日LeetCode

作者: wwndbk (黑人問號)   2023-10-31 14:58:52
https://leetcode.com/problems/find-the-original-array-of-prefix-xor/
2433. Find The Original Array of Prefix Xor
給你一個陣列 pref,
要求出前面arr[0]到arr[n-1]的xor再xor哪個數可以變成pref[n]
Input: pref = [5,2,0,3,1]
Output: [5,7,2,3,2]
Explanation: From the array [5,7,2,3,2] we have the following:
- pref[0] = 5.
- pref[1] = 5 ^ 7 = 2.
- pref[2] = 5 ^ 7 ^ 2 = 0.
- pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3.
- pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1.
思路:
arr[0]到arr[n-1]的xor就是 pref[n-1],
已知 pref[n-1] ^ arr[n] = pref[n] ,
交換過後可以知道arr[n] = pref[n-1] ^ pref[n]
rust code:

Links booklink

Contact Us: admin [ a t ] ucptt.com