387. First Unique Character in a String QuestionEditorial Solution

原创 2016年08月29日 23:48:10

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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

LeetCode 387. First Unique Character in a String 解题报告

LeetCode 387. First Unique Character in a String 解题报告

LeetCode之路:387. First Unique Character in a String

LeetCode之路:387. First Unique Character in a String 一、引言这道题题意非常简单,却让我思考了很久,或许难的并不是这道题,是纠结于解题思路的那种困扰吧。...

LeetCode-387. First Unique Character in a String (Java)

Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex...
  • NOadu
  • NOadu
  • 2017-06-25 09:58
  • 134

leetcode.387. First Unique Character in a String--字符串首个不重复的字母

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

LeetCode笔记:387. First Unique Character in a String

找到一个字符串中第一个只出现一次的字母

Leetcode 387. First Unique Character in a String 第一个唯一字符 解题报告

1 解题思想这题的意思是,给一个字符串,找出第一个只出现一次的字符的位置。这道题限定了只有小写字符,所以用一个小写字符的数组去做标志位,记录某个字符是否出现,如果出现了一次那么位置在哪里,或者出现了多...
  • MebiuW
  • MebiuW
  • 2016-08-24 13:02
  • 1210

关于LeetCode中First Unique Character in a String一题的理解

题目如下: Given a string, find the first non-repeating character in it and return it's index. If it do...

LeetCode - First Unique Character in a String

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

LeetCode17 17. Letter Combinations of a Phone Number My Submissions QuestionEditorial Solution

Given a digit string, return all possible letter combinations that the number could represent. A ...

End of string character in Java

原文转自:http://stackoverflow.com/questions/17815755/end-of-string-character-in-java Stack Over...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)