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