Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5d92e86

Browse files
committed
solution 6 added with test case
1 parentb432a20 commit5d92e86

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

‎src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
importrun4from'./solutions/4';
1+
importrun6from'./solutions/6';
22

3-
run4();
3+
run6();

‎src/solutions/6/convert.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
importconvertfrom"./convert";
2+
3+
test('Solution 6: Zigzag Convert',()=>{
4+
expect(convert('PAYPALISHIRING',3)).toBe('PAHNAPLSIIGYIR');
5+
expect(convert('PAYPALISHIRING',4)).toBe('PINALSIGYAHRPI');
6+
expect(convert('PAYPALISHIRING',5)).toBe('PHASIYIRPLIGAN');
7+
})

‎src/solutions/6/convert.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
constconvert=(s:string,numRows:number):string=>{
2+
if(numRows<2){
3+
returns;
4+
}
5+
constwidth=numRows*2-2;
6+
letresult:string='';
7+
leti;
8+
letj;
9+
for(letrow=0;row<numRows;row++){
10+
i=row;
11+
j=width-2*row;
12+
while(i<s.length){
13+
if(j>0){
14+
result+=s[i];
15+
}
16+
i+=j;
17+
j=width-j;
18+
}
19+
}
20+
returnresult;
21+
}
22+
23+
exportdefaultconvert;

‎src/solutions/6/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
importconvertfrom'./convert';
2+
3+
construn=()=>{
4+
constinput :string='PAYPALISHIRING';
5+
constnumRows :number=3;
6+
constexpectedOutput :string='PAHNAPLSIIGYIR';
7+
8+
9+
console.info("Input : ",input)
10+
console.info("Target: ",numRows)
11+
console.info("Expected Output: ",expectedOutput)
12+
console.log("=============================")
13+
console.time("execution")
14+
console.log("Output:::::",convert(input,numRows))
15+
console.timeEnd("execution")
16+
}
17+
18+
exportdefaultrun;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp