LeetCode

来源:互联网 发布:网络惊魂下载 编辑:程序博客网 时间:2024/06/05 06:32

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.

一开始没看懂题,一脸懵逼地以为t是s的子串,然后在那里使劲yy,yy不出来orzzzzzzzzz

最后怀疑人生,这真的是easy么QAQ。。。后来看了评论区。。才懂了题意。。心情好复杂。。。不就是,,,数组中只出现一次的数的改版。。。。

题意:t为s的乱序,然后新添加了一个字母,求这个字母

时间复杂度O(n),空间复杂度O(1)

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

原创粉丝点击