ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

LeeCode 389. 找不同

LeeCode 389. 找不同 给定两个字符串s和t它们只包含小写字母。字符串t由字符串s随机重排然后在随机位置添加一个字母。请找出在t中被添加的字母。示例 1输入s abcd, t abcde输出e解释e 是那个被添加的字母。示例 2输入s , t y输出y提示0 s.length 1000t.length s.length 1s和t只包含小写字母答案char findTheDifference(char* s, char* t) { int arr1[26] {0}; int arr2[26] {0}; int len_s strlen(s); int len_t strlen(t); for (int i 0; i len_s; i) { arr1[s[i] - a]; } for (int i 0; i len_t; i) { arr2[t[i] - a]; } for (int i 0; i 26; i) { if (arr1[i] ! arr2[i]) { return a i; } } return 0; }测试代码:void testLeeCode389(void) { char* s abcd; char* t abcde; printf(多出来的字符是: %c\n, findTheDifference(s, t)); }运行ok. 提交到LeeCodeok.但是编译器其实是有异常警告字符串最好改成const char* 类型 或者这样 用字符数组ok.
返回列表