首页 > 其他 > 详细

LeetCode 389. 找不同

时间:2020-04-11 01:43:44      阅读:78      评论:0      收藏:0      [点我收藏+]

题目链接:https://leetcode-cn.com/problems/find-the-difference/

给定两个字符串 s 和 t,它们只包含小写字母。

字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。

请找出在 t 中被添加的字母。

 

示例:

输入:
s = "abcd"
t = "abcde"

输出:
e

解释:
‘e‘ 是那个被添加的字母。

 1 class Solution {
 2 public:
 3     char findTheDifference(string s, string t) {
 4         int cnt[26]={0};
 5         int len1=s.length(),len2=t.length();
 6         int i,j;
 7         for(i=0;i<len1;i++){
 8             int tmp=s[i]-a;
 9             cnt[tmp]++;
10         }
11         for(i=0;i<len2;i++){
12             int tmp=t[i]-a;
13             cnt[tmp]--;
14         }
15         for(i=0;i<26;i++){
16             if(cnt[i]!=0){
17                 char ch=a+i;
18                 return ch;
19             }
20         }
21         return t[len2-1];
22     }
23 };

 

LeetCode 389. 找不同

原文:https://www.cnblogs.com/shixinzei/p/12677362.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!