387. First Unique Character in a String(C语言)

xiaoxiao2021-02-28  100

看有没有重复的字母出现,没有的话输出第一个没重复的字母的下标,都是重复的话就输出-1

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.

int firstUniqChar(char* s) { int i=0,j=0; int len=strlen(s); int freq[26]; for(i=0;i<26;i++){ freq[i]=0; } for(i=0;i<len;i++){ freq[s[i]-'a']++; } for(i=0;i<len;i++){ if(freq[s[i]-'a']==1){ return i; } } return -1; }

转载请注明原文地址: https://www.6miu.com/read-73833.html

最新回复(0)