Leetcode#389. Find the Difference(位运算=异或)

xiaoxiao2021-02-28  87

                                                                                

                                                                       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. 题意:给你两个字符串s,t,t是有s字符串在任意位置添加一个字符得来的,求出这个添加的字符串。

异或,但是字符没有办法异或,那是你可以把它转换为(int),最后输出将(int)转换为char型输出。

我的代码:

class Solution { public: char findTheDifference(string s, string t) { int res=0; for (int i=0;i<t.size();i++) { res=res^int(s[i]); res=res^int(t[i]); } return char(res); } };

转载请注明原文地址: https://www.6miu.com/read-96205.html

最新回复(0)