Zhilong Li пре 4 година
родитељ
комит
4d01bf628c
2 измењених фајлова са 33 додато и 1 уклоњено
  1. 31 0
      387_first_unique_character_in_a_string.cpp
  2. 2 1
      CMakeLists.txt

+ 31 - 0
387_first_unique_character_in_a_string.cpp

@@ -0,0 +1,31 @@
+#include <string>
+#include <vector>
+#include <iostream>
+
+using namespace std;
+
+class Solution
+{
+public:
+    int firstUniqChar(string s)
+    {
+        vector<int> table(26);
+        for (int i = 0; i < s.size(); i++)
+        {
+            table[s[i] - 'a'] += 1;
+        }
+        for (int i = 0; i < s.size(); i++)
+        {
+            if (table[s[i] - 'a'] == 1)
+                return i;
+        }
+        return -1;
+    }
+};
+
+int main()
+{
+    string s = "altcleetcodde";
+    Solution sol;
+    cout << sol.firstUniqChar(s) << endl;
+}

+ 2 - 1
CMakeLists.txt

@@ -5,4 +5,5 @@ set(CMAKE_CXX_STANDARD 17)
 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY executables)
 add_executable(74_search_a_2d_matrix 74_search_a_2d_matrix.cpp)
 add_executable(383_ransom_note 383_ransom_note.cpp)
-add_executable(242_valid_anagram 242_valid_anagram.cpp)
+add_executable(242_valid_anagram 242_valid_anagram.cpp)
+add_executable(387_first_unique_character_in_a_string 387_first_unique_character_in_a_string.cpp)