作者yam276 (史萊哲林的優等生)
看板Marginalman
標題Re: [閒聊] 每日LeetCode
時間Tue Sep 26 16:12:39 2023
※ 引述《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
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.251.123.162 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1695715961.A.8ED.html
推 Rushia: 大師 09/26 16:18
推 NTHUlagka: 大師 09/26 16:38