精華區beta Marginalman 關於我們 聯絡資訊
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: ------------------------------------------------ impl Solution { pub fn find_array(pref: Vec<i32>) -> Vec<i32> { let mut result: Vec<i32> = Vec::new(); let mut now = 0; for it in pref { result.push(it ^ now); now = it; } result } } 好怪的題目 -- snoopy可愛可愛愛 https://imgur.com/ZMUxfLq.jpg
https://imgur.com/KrZuxZw.jpg
https://imgur.com/WVJ7zSi.jpg
https://imgur.com/yOrRYiA.jpg
https://imgur.com/q4h1xRZ.jpg
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.113.169.2 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1698735534.A.701.html ※ 編輯: wwndbk (140.113.169.2 臺灣), 10/31/2023 14:59:41