欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

387. First Unique Character in a String QuestionEditorial Solution

发布时间:2023/12/15 47 豆豆
生活随笔 收集整理的这篇文章主要介绍了 387. First Unique Character in a String QuestionEditorial Solution 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1.

Examples:

s = “leetcode”
return 0.

s = “loveleetcode”,
return 2.
Note: You may assume the string contain only lowercase letters.

思路,统计每个每个字母出现的次数到一个数组中,数组大小为26个小写字母。

int firstUniqChar(char* s) {int i;char *str;int count[26];memset(count, 0, 26 * sizeof(int));for(str = s; *str != '\0'; str++) {i = *str - 'a';count[i]++;}for(str = s; *str != '\0'; str++) {i = *str - 'a';if(count[i] == 1)return str - s;}return -1; } 创作挑战赛新人创作奖励来咯,坚持创作打卡瓜分现金大奖

总结

以上是生活随笔为你收集整理的387. First Unique Character in a String QuestionEditorial Solution的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。