Re: [闲聊] 每日LeetCode

楼主: yam276 ('_')   2023-11-01 15:26:21
※ 引述《ZooseWu (动物园 公告)》之铭言:
: ※ 引述《wwndbk (snoopy养的狗)》之铭言:
: : https://leetcode.com/problems/find-the-original-array-of-prefix-xor/
: : 2433. Find The Original Array of Prefix Xor
: 思路:
: 思路同上篇
: 这题应该是考使用者会不会 xor 的一些观念
: 例如 a = b ^ c => b = a ^ c 之类的
: TS code:
: function findArray (pref: number[]): number[] {
: const result: number[] = [pref[0]]
: for (let i = 1; i < pref.length; i++) {
: result.push(pref[i] ^ pref[i - 1])
: }
: return result
: }
这题看起来好快乐喔
本来想用输入Vec来当输出
impl Solution {
pub fn find_array(mut pref: Vec<i32>) -> Vec<i32> {
for index in (1..pref.len()).rev() {
pref[index] ^= pref[index - 1];
}
pref
}
}
但空间O不太理想
反而创新Vec省一点
impl Solution {
pub fn find_array(pref: Vec<i32>) -> Vec<i32> {
let mut result = vec![pref[0]];
for i in 1..pref.len() {
result.push(pref[i-1] ^ pref[i]);
}
retult
}
}

Links booklink

Contact Us: admin [ a t ] ucptt.com