以下是我的答案
class Solution{
public:
char findTheDifference(string s, string t){
for (int i = 0; i < t.size(); i++)
{
if (s.find(t[i])==-1)
return t[i];
}
return NULL;
}
想请问各位大神,这段代码中是否有明显错误,以及其中的不足之处
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.
#
http://blog.csdn.net/zmq570235977/article/details/51589146
NULL不是Java关键字吧?null才是。
另外你这个算法性能不够高(O(n^2)),还可以提升到O(n)。
利用Hash思想,扫描s建立一个Hash表(长度为256,对应的是字符的ASCII码),这一步的复杂度是O(n)。之后再扫描一遍字符串t,以ASCII码作为下标,Hash表对应位置不为0则代表没有在s中出现,所以要输出。这一步的复杂度也是O(n)。总的复杂度是O(n)