389.Find the Difference

桃扇骨 2022-06-09 05:38 317阅读 0赞
  1. /*
  2. Given two strings s and t which consist of only lowercase letters.
  3. String t is generated by random shuffling string s and then add one more letter at a random position.
  4. Find the letter that was added in t.
  5. Example:
  6. Input:
  7. s = "abcd"
  8. t = "abcde"
  9. Output:
  10. e
  11. Explanation:
  12. 'e' is the letter that was added.
  13. */
  14. char findTheDifference(char* s, char* t) {
  15. int i,j;
  16. char add;
  17. int n = strlen(t);
  18. for(i = 0; i <= n-1; i++)//将相同的数都置换为1,避免遇到数组中有多个相同字母,这样可以一一对应,筛掉相同的,留下不同的
  19. for(j=0;j<= n-2;j++)
  20. if(t[i]==s[j])
  21. {
  22. t[i] = 49;
  23. s[j] = 49;
  24. break;
  25. }
  26. for(i=0;i<n;i++)//找出没有被替换的数,即多出来的数
  27. if(t[i]!=49)
  28. return t[i];
  29. return;
  30. }

发表评论

表情:
评论列表 (有 0 条评论,317人围观)

还没有评论,来说两句吧...

相关阅读