Re: [閒聊] 每日LeetCode

作者: yam276 ('_')   2023-09-26 16:12:39
※ 引述《Rushia (みけねこ的鼻屎)》之銘言:
: https://leetcode.com/problems/find-the-difference/description
: 389. Find the Difference
: 給你一個字串 s 和 t,t 是 s 字串字元亂序並加上一個字元組成的,返回這個被加上
: 的字元。
思路:
用XOR
因為(a⊕a)⊕(b⊕b)⊕c=0⊕0⊕c=c
impl Solution {
pub fn find_the_difference(s: String, t: String) -> char {
let mut result = 0u8;
for c in s.bytes() {
result ^= c;
}
for c in t.bytes() {
result ^= c;
}
result as char
}
}
作者: Rushia (みけねこ的鼻屎)   2023-09-26 16:18:00
大師
作者: NTHUlagka (拉卡)   2023-09-26 16:38:00
大師

Links booklink

Contact Us: admin [ a t ] ucptt.com