LeetCode OJ-387. First Unique Character in a String

原创 2016年08月31日 16:24:58
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 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 res = -1;
    int rec[26] = { 0 };
    int len = (int) strlen(s);
    int i;
    for (i = 0; i < len; ++i) {
        ++rec[(int) (s[i] - 'a')];
    }
    
    for (i = 0; i < len; ++i) {
        if (rec[(int) (s[i] - 'a')] == 1) {
            res = i;
            break;
        }
    }
    
    return res;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

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 一、引言这道题题意非常简单,却让我思考了很久,或许难的并不是这道题,是纠结于解题思路的那种困扰吧。...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

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
  • 102

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
  • 1124

关于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’...

leetcode:Unique Substrings in Wraparound String

Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so s wi...

LeetCode 题解(Week 10): 467. Unique Substrings in Wraparound String

原题目Consider the string s to be the infinite wraparound string of “abcdefghijklmnopqrstuvwxyz”, so s ...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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