LeetCode 387. First Unique Character in a String

https://leetcode.com/problems/first-unique-character-in-a-string/description/

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.

  • 字符串判重简单题。基本思想就是用hash table。
  • 第一种方法只简单考虑26个ACSII英文字母,第二种扩展到更普遍的情况用map来处理Unicode字符,第三种则是只扫描输入字符串一次,然后扫描hash table一次。
  • 注意STL算法的使用。
  • unordered_map::find - C++ Reference
    • http://www.cplusplus.com/reference/unordered_map/unordered_map/find/
  • min - C++ Reference
    • http://www.cplusplus.com/reference/algorithm/min/?kw=min
  • <climits> (limits.h) - C++ Reference
    • http://www.cplusplus.com/reference/climits/
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 
 9 #include <iostream>
10 #include <vector>
11 #include <unordered_map>
12 using namespace std;
13 
14 class Solution {
15 public:
16     // Only considering 26 ASCII characters
17     int firstUniqChar(string s) {
18         vector<int> dict(26, 0);
19         
20         for (auto& ch : s) {
21             ++ dict[ch - 'a'];
22         }
23         
24         for (int i = 0; i < s.size(); i ++) {
25             if (dict[s.at(i) - 'a'] == 1)
26                 return i;
27         }
28         
29         return -1;
30     }
31 
32     // Common way for UNICODE characters
33     int firstUniqChar2(string s) {
34         unordered_map<char, int> dict; // character and index
35         
36         for (auto& ch : s) {
37             ++ dict[ch];
38         }
39         
40         for (int i = 0; i < s.size(); i ++) {
41             if (dict[s.at(i)] == 1)
42                 return i;
43         }
44         
45         return -1;
46     }
47 
48     // Only traverse the string once
49     int firstUniqChar3(string s) {
50         unordered_map<char, int> dict;
51         
52         for (int i = 0; i < s.size(); i ++) {
53             if (dict.end() == dict.find(s.at(i))) // pay attention to the condition check
54                 dict[s.at(i)] = i;
55             else
56                 dict[s.at(i)] = INT_MAX; // for the dup
57         }
58         
59         int res = INT_MAX;
60         
61         for (auto& it : dict) {
62             res = min(res, it.second);
63         }
64         
65         return res == INT_MAX ? -1 : res;
66     }
67 };
68 
69 int main(int argc, char* argv[])
70 {
71     Solution    testSolution;
72     
73     vector<string> strs {"", "leetcode", "loveleetcode"};
74 
75     /*
76      -1
77      -1
78      -1
79      0
80      0
81      0
82      2
83      2
84      2
85      */
86     for (auto s : strs) {
87         cout << testSolution.firstUniqChar(s) << endl;
88         cout << testSolution.firstUniqChar2(s) << endl;
89         cout << testSolution.firstUniqChar3(s) << endl;
90     }
91     
92     return 0;
93 }
View Code

 

posted on 2018-02-02 16:22  浩然119  阅读(207)  评论(0编辑  收藏  举报