mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 02:53:31 +08:00
Merge pull request #1253 from xiaofei-2020/greed19
添加(0763.划分字母区间.md):增加typescript版本
This commit is contained in:
@ -229,6 +229,31 @@ var partitionLabels = function(s) {
|
||||
};
|
||||
```
|
||||
|
||||
### TypeScript
|
||||
|
||||
```typescript
|
||||
function partitionLabels(s: string): number[] {
|
||||
const length: number = s.length;
|
||||
const resArr: number[] = [];
|
||||
const helperMap: Map<string, number> = new Map();
|
||||
for (let i = 0; i < length; i++) {
|
||||
helperMap.set(s[i], i);
|
||||
}
|
||||
let left: number = 0;
|
||||
let right: number = 0;
|
||||
for (let i = 0; i < length; i++) {
|
||||
right = Math.max(helperMap.get(s[i])!, right);
|
||||
if (i === right) {
|
||||
resArr.push(i - left + 1);
|
||||
left = i + 1;
|
||||
}
|
||||
}
|
||||
return resArr;
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
Reference in New Issue
Block a user