leetcode389

1、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.

class Solution {
public:
    char findTheDifference(string s, string t) {
        int sum=0,sum1=0;
        for(int i=0;i<s.length();i++){
            sum=sum+s[i];
        }
        for(int i=0;i<t.length();i++){
            sum1=sum1+t[i];
        }
        return sum1-sum;
    }
};

這個題類似與上一道題,把字母的問題轉化成ASCII碼加減~~簡單高效~

發佈了59 篇原創文章 · 獲贊 2 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章