[LeetCode By Go 25]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.

解題思路

使用異或的原理

a ^ 0 = a
a ^ a = 0
a ^ b ^ c = a ^ ( b ^ c )

因此,將s,t兩個字符串的所有字符轉為byte類型進行異或,最后的結果就是多出來的字符

代碼

findTheDifference.go

package _389_Find_the_Difference

func FindTheDifference(s string, t string) byte {
    var ret byte
    for _, v := range s {
        ret = ret ^ byte(v)
    }

    for _, v := range t {
        ret = ret ^ byte(v)
    }
    return ret
}

測試

findTheDifference_test.go

package _389_Find_the_Difference

import "testing"

func TestFindTheDifference(t *testing.T) {
    var tests = []struct{
        s string
        t string
        output byte
    }{
        {"abcd","abcde", byte('e')},
    }

    for _, test := range tests {
        ret := FindTheDifference(test.s, test.t)

        if ret == test.output {

        }
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,160評論 0 23
  • 標簽(空格分隔): 編程 Go官方文檔 Using the tour 1.1 Hello, 世界 Welcome...
    uangianlap閱讀 1,647評論 0 5
  • 藝評新銳“許友維” ——從城坊間、鄉(xiāng)土系列人士之“明輝畫廊” 火山 在從化的一本土綜合網(wǎng)站——神采飛揚網(wǎng),與許友維...
    朱明云閱讀 648評論 2 6
  • 今天是大年初一給大伙兒拜年。 我自己的習慣是每個大年三十夜深人靜的時候都要找個地方反思一下過去的一年。 昨天我腦子...
    Curtis2019閱讀 184評論 0 0
  • 昨天,周五!美麗周末的開啟~可是下班時間倒數(shù)時,莫名其妙的被空虛侵襲,內心一萬個“浪”在奔騰,可是,孤身一人,何去...
    卿FF閱讀 867評論 0 0

友情鏈接更多精彩內容