简单题,没什么好说的,Hash数组+两次遍历可破;
int firstUniqChar(string s) { for (int i = 0; i < s.size(); i++) { hashtable[s[i] - 'a']++; } int index = 0; for (int i = 0; i < s.size(); i++) { if (hashtable[s[i] - 'a'] == 1) { index = i; break; } } return index; }
2024-03-16 23:37:40
简单题,没什么好说的,Hash数组+两次遍历可破;
int firstUniqChar(string s) { for (int i = 0; i < s.size(); i++) { hashtable[s[i] - 'a']++; } int index = 0; for (int i = 0; i < s.size(); i++) { if (hashtable[s[i] - 'a'] == 1) { index = i; break; } } return index; }