leetCode387.FirstUniqueCharacterinaString字符串

387. First Unique Character in a String

成都创新互联公司2013年成立,先为祁连等服务建站,祁连等地企业,进行企业商务咨询服务。为祁连企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

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.

此题比较简单

代码如下:

class Solution {
public:
    int firstUniqChar(string s) {
        set myset;
        if (s.empty())
        {
            return -1;
        }
        for(int i = 0 ; i < s.size();i++)
        {
            char c = s[i];
            int j;
            if(myset.find(c) != myset.end())
            {
                continue;
            }
            for(j = i + 1; j < s.size();j++)
            {
                if(s[j] == s[i])
                {
                    myset.insert(c);
                    break;
                }
            }
            if(j == s.size() )
            {
                return i;
            }
        }
        return -1;
    }
};

2016-08-24 23:58:39


新闻名称:leetCode387.FirstUniqueCharacterinaString字符串
浏览路径:http://hbruida.cn/article/jspccs.html