您好,登錄后才能下訂單哦!
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.
此題比較簡單
代碼如下:
class Solution { public: int firstUniqChar(string s) { set<char > 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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。