389 find the difference
生活随笔
收集整理的這篇文章主要介紹了
389 find the difference
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
問題:Given two strings s and t which consist of only lowercase letters.
String t is generated by random shuffling string s and then add one more letter at a random position.
Find the letter that was added in t.
Example:
Input:
s = “abcd”
t = “abcde”
Output:
e
Explanation:
‘e’ is the letter that was added.
思路:使用hashmap,計算s中每個字符出現的次數。再遍歷字符串t,每遇到一個字符,將map中的值減1。如果字符不出現在map,或者計數為0,就是后來加入的字符。
收獲:1 用異或。a^b^a=b,a^b^b=a,a^b^a^b=0。交換兩個int,不用第三個空間的做法是:a=a^b,b=a^b,a=a^b。
參考資料
1 題目
2 討論
總結
以上是生活随笔為你收集整理的389 find the difference的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 轻松实现无刷新三级联动菜单[VS2005
- 下一篇: 京东快捷支付的安全隐患