add: 第一个只出现一次的字符
This commit is contained in:
parent
9c2a165583
commit
d348c63824
1
.vscode/settings.json
vendored
1
.vscode/settings.json
vendored
@ -8,6 +8,7 @@
|
|||||||
"lcci",
|
"lcci",
|
||||||
"lcof",
|
"lcof",
|
||||||
"lcov",
|
"lcov",
|
||||||
|
"leetcode",
|
||||||
"lrloseumgh",
|
"lrloseumgh",
|
||||||
"mincost",
|
"mincost",
|
||||||
"nums",
|
"nums",
|
||||||
|
@ -287,6 +287,12 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重
|
|||||||
|
|
||||||
- LintCode 685. 数据流中第一个唯一的数字 <https://www.lintcode.com/problem/first-unique-number-in-data-stream/description>
|
- 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)
|
- [最大矩阵](src/stack/maximal-rectangle.js)
|
||||||
|
18
src/array/first-unique-character-in-a-string.js
Normal file
18
src/array/first-unique-character-in-a-string.js
Normal 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] || ' '
|
||||||
|
}
|
5
test/array/first-unique-character-in-a-string.test.js
Normal file
5
test/array/first-unique-character-in-a-string.test.js
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import { firstUniqChar } from '../../src/array/first-unique-character-in-a-string'
|
||||||
|
|
||||||
|
test('第一个只出现一次的字符', () => {
|
||||||
|
expect(firstUniqChar('leetcode')).toBe('l')
|
||||||
|
})
|
Loading…
Reference in New Issue
Block a user