add: 第一个只出现一次的字符

This commit is contained in:
yi-ge 2020-05-14 18:26:00 +08:00
parent 9c2a165583
commit d348c63824
4 changed files with 30 additions and 0 deletions

View File

@ -8,6 +8,7 @@
"lcci",
"lcof",
"lcov",
"leetcode",
"lrloseumgh",
"mincost",
"nums",

View File

@ -287,6 +287,12 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重
- LintCode 685. 数据流中第一个唯一的数字 <https://www.lintcode.com/problem/first-unique-number-in-data-stream/description>
- [第一个只出现一次的字符](src/array/first-unique-character-in-a-string.js)
- LeetCode 面试题50. 第一个只出现一次的字符 <https://leetcode-cn.com/problems/di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof/>
- LeetCode 387. 字符串中的第一个唯一字符 <https://leetcode-cn.com/problems/first-unique-character-in-a-string/>
- LintCode 209. 第一个只出现一次的字符 <https://www.lintcode.com/problem/first-unique-character-in-a-string/description>
## 栈
- [最大矩阵](src/stack/maximal-rectangle.js)

View File

@ -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] || ' '
}

View File

@ -0,0 +1,5 @@
import { firstUniqChar } from '../../src/array/first-unique-character-in-a-string'
test('第一个只出现一次的字符', () => {
expect(firstUniqChar('leetcode')).toBe('l')
})