LeetCode 389. 找不同

题目描述

389. 找不同

image-20250420074652014

思路分析

异或运算

参考代码

1
2
3
4
5
6
7
8
9
10
func findTheDifference(s string, t string) byte {
    var res byte = 0
    for i := 0; i < len(s); i++ {
        res ^= s[i]
    }
    for i := 0; i < len(t); i++ {
        res ^= t[i]
    }
    return res
}
  • 时间复杂度:O(n),其中 n 是字符串 s 的长度。
  • 空间复杂度:O(1),只使用了常数级别的额外变量。
1
write your code here

➡️ 点击查看 Java 题解

1
write your code here
本文作者:
本文链接: https://hgnulb.github.io/blog/2021/47584640
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处!