Find the Difference(leetcode389)
生活随笔
收集整理的這篇文章主要介紹了
Find the Difference(leetcode389)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
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: eExplanation: 'e' is the letter that was added.?
//既然都是小寫字母 那么用之前的數(shù)組也是解決的 //這種方法與使用map是相同的 public static char findTheDifference(String s, String t) {int[] arr = new int[26];for (int i = 0; i < t.length(); i++) {arr[t.charAt(i) - 'a']++;}for (int i = 0; i < s.length(); i++) {--arr[s.charAt(i)-'a'];}for (int i =0;i< arr.length;i++){if(arr[i]>0){return (char)(i+'a');}}return ' '; }?
//原來還可以這么寫呀 public static char findTheDifference2(String s, String t) {char c = 0;for (int i = 0; i < s.length(); ++i) {c ^= s.charAt(i);}for (int i = 0; i < t.length(); ++i) {c ^= t.charAt(i);}return c; }?
//寫的看上去簡略一點(diǎn) 少循環(huán)一次 public static char findTheDifference3(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; }?
git:https://github.com/woshiyexinjie/leetcode-xin
?
轉(zhuǎn)載于:https://my.oschina.net/u/2277632/blog/2999403
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的Find the Difference(leetcode389)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring框架知识复习之二
- 下一篇: [洛谷P1341]无序字母对