☀（day55：C5）

✨代码注释

✨代码注释

✨代码注释

⭐示例 1：

⭐示例 2：

解题思路：

解法一：常规解法

代码实现

``````# include
# include

char findTheDifference(char* s, char* t)
{
int arr[26];
memset(arr, 0, sizeof(arr));
int n = strlen(s);
int m = strlen(t);

for (int i = 0; i < n; i++)
arr[s[i] - 'a']++;

for (int i = 0; i < m; i++)
{
arr[t[i] - 'a']--;
if (arr[t[i] - 'a'] < 0)
return t[i];
}
return ' ';
}

void main (void)
{
char s[4] = "abc";
char t[5] = "abce";
char differen;

differen = findTheDifference(s, t);
printf("%c", differen);
}``````

✨代码注释

``````# include
# include

char findTheDifference(char* s, char* t)
{
int arr[26];
// 使用memset函数将数组arr的元素全部初始化为0
memset(arr, 0, sizeof(arr));
// 使用strlen方法获取s和t字符串长度并分别赋值给n，m
int n = strlen(s);
int m = strlen(t);

for (int i = 0; i < n; i++)
// 遍历s的字母并将字母对应下标i的位置+1
arr[s[i] - 'a']++;

for (int i = 0; i < m; i++)
{
// 遍历t的字母并将字母对应下标i的位置-1
arr[t[i] - 'a']--;
// 判断-1后改位置上的数是否<0，<0则找到不同的字母
if (arr[t[i] - 'a'] < 0)
return t[i];
}
return ' ';
}

void main (void)
{
char s[4] = "abc";
char t[5] = "abce";
char differen;

differen = findTheDifference(s, t);
printf("%c", differen);
}``````

buffer：为指针或是数组,

c：是赋给buffer的值,

count：是buffer的长度.

解法二：ASCII码求和

代码实现

``````# include
# include

char findTheDifference(char* s, char* t)
{
int n = strlen(s), m = strlen(t);
int ASCII_s = 0;
int ASCII_t = 0;

for (int i = 0; i < n; i++)
ASCII_s += s[i];

for (int i = 0; i < m; i++)
ASCII_t += t[i];

return ASCII_t - ASCII_s;
}

void main (void)
{
char s[4] = "abc";
char t[5] = "abce";
char difference;

difference = findTheDifference(s, t);
printf("%c", difference);
}``````

✨代码注释

``````# include
# include

char findTheDifference(char* s, char* t)
{
// 使用strlen方法获取s和t字符串长度并分别赋值给n，m
int n = strlen(s), m = strlen(t);
// ASCII_s,ascii_t分别表示s和t的总ASCII值
int ASCII_s = 0;
int ASCII_t = 0;

// 求s中所有字母的ASCII值
for (int i = 0; i < n; i++)
ASCII_s += s[i];
// 求t中所有字母的ASCII值
for (int i = 0; i < m; i++)
ASCII_t += t[i];

return ASCII_t - ASCII_s;
}

void main (void)
{
char s[4] = "abc";
char t[5] = "abce";
char difference;

difference = findTheDifference(s, t);
printf("%c", difference);
}``````

解法三：位运算

再位运算中，按位异或有个特点：当使用按位异或不断运算具相同的数时最后的结果为0.

0^1 = 1, 1^2 = 3, 3^4 = 7

7^1 = 6, 6^2 = 4, 4^4 = 0

代码实现

``````# include
# include

char findTheDifference(char* s, char* t)
{
int n = strlen(s), m = strlen(t);
int result = 0;
for (int i = 0; i < n; i++)
result ^= s[i];

for (int i = 0; i < m; i++)
result ^= t[i];

return result;
}

void main (void)
{
char s[4] = "abc";
char t[5] = "abce";
char difference;

difference = findTheDifference(s, t);
printf("%c", difference);
}``````

✨代码注释

``````# include
# include

char findTheDifference(char* s, char* t)
{
// 使用strlen方法获取s和t字符串长度并分别赋值给n，m
int n = strlen(s), m = strlen(t);
int result = 0;
// 对s中所有字母的ascii值进行按位异或运算
for (int i = 0; i < n; i++)
result ^= s[i];

// 对t中所有字母的ascii值进行按位异或运算
for (int i = 0; i < m; i++)
result ^= t[i];

return result;
}

void main (void)
{
char s[4] = "abc";
char t[5] = "abce";
char difference;

difference = findTheDifference(s, t);
printf("%c", difference);
}``````

❄❄❄❄❄❄❄❄❄❄❄❄❄❄❄❄❄❄❄❄end❄❄❄❄❄❄❄❄❄❄❄❄❄❄❄❄❄❄❄❄❄❄❄❄❄❄