387. First Unique Character in a String QuestionEditorial Solution
生活随笔
收集整理的這篇文章主要介紹了
387. First Unique Character in a String QuestionEditorial Solution
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
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 = “l(fā)eetcode”
return 0.
s = “l(fā)oveleetcode”,
return 2.
Note: You may assume the string contain only lowercase letters.
思路,統(tǒng)計每個每個字母出現(xiàn)的次數(shù)到一個數(shù)組中,數(shù)組大小為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; } 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的387. First Unique Character in a String QuestionEditorial Solution的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 新款MacBook Pro发布 搭载M2
- 下一篇: a.out、coff、elf三种文件格式