add: 第一个只出现一次的字符
This commit is contained in:
1 parent
9c2a165583
commit
d348c63824
4 files changed
+30
No files matched your search
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* @param {string} s
|
||||
* @return {character}
|
||||
*/
|
||||
export const firstUniqChar = function (s) {
|
||||
const due = new Set()
|
||||
const queue = new Set()
|
||||
for (const n of s) {
|
||||
if (queue.has(n)) {
|
||||
queue.delete(n)
|
||||
due.add(n)
|
||||
} else if (!due.has(n)) {
|
||||
queue.add(n)
|
||||
}
|
||||
}
|
||||
|
||||
return Array.from(queue)[0] || ' '
|
||||
}
|
||||
Reference in new issue
Block a user