推 nozomizo: 你是大師 09/25 13:13
https://leetcode.com/problems/find-the-difference/description
389. Find the Difference
給你一個字串 s 和 t,t 是 s 字串字元亂序並加上一個字元組成的,返回這個被加上
的字元。
思路:
1.對s和t的字元計數,一個遞增一個遞減。
2.數量不為0的字元表示是多加的字元,返回即可。
Java Code:
--------------------------------
class Solution {
public char findTheDifference(String s, String t) {
int[] count = new int[26];
for (char c : s.toCharArray()) {
count[c - 'a']++;
}
for (char c : t.toCharArray()) {
count[c - 'a']--;
}
for (int i = 0; i < 26; i++) {
if (count[i] < 0) {
return (char)('a' + i);
}
}
return ' ';
}
}
--------------------------------
我這輩子只寫得出eazy了 哈
--
https://i.imgur.com/bFRiqA3.jpg
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.100.73.13 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1695617804.A.076.html