387. First Unique Character in a String
成都創(chuàng)新互聯(lián)公司2013年成立,先為祁連等服務(wù)建站,祁連等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為祁連企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
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) { setmyset; 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