- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzigzag-conversion.ts
32 lines (26 loc) · 755 Bytes
/
zigzag-conversion.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/** Z 字形变换
* @description
* @author tangc1
* @date 2022-06-06 10:14:20
*/
exportfunctionzconvert(s: string,numRows: number){
if(s===''||numRows<2)returns
constarr: string[]=newArray(numRows).fill('')
letindex=0
letflag=-1
for(leti=0;i<s.length;i++){
constc=s[i]
arr[index]+=c
if(index===0||index===numRows-1){
flag=-flag
}
index+=flag
}
returnarr.join('')
}
// zconvert("PAYPALISHIRING", 3)
// zconvert("PINALSIGYAHR", 4)
// console.info(zconvert("PAYPALISHIRING", 4));
// console.info(zconvert("PAYPALISHIRING", 3));
// console.info(zconvert("PINALSIGYAHR", 4));
// console.info(zconvert("LEETCODE", 3));