https://leetcode.com/problems/find-the-difference/
位运算
最优解:
public char findTheDifference(String s, String t) {
int n = t.length();
char c = t.charAt(n - 1);
for (int i = 0; i < n - 1; ++i) {
c ^= s.charAt(i);
c ^= t.charAt(i);
}
return c;
}
我的解:
public class Solution {
public char findTheDifference(String t, String s) {
char sum = 0;
for (int i = 0; i < s.length(); i++) {
sum += s.charAt(i);
}
for (int i = 0; i < t.length(); i++) {
sum -= t.charAt(i);
}
return sum;
}
}
转载请注明原文地址: https://ju.6miu.com/read-1000195.html