File tree Expand file tree Collapse file tree 2 files changed +48
-0
lines changed Expand file tree Collapse file tree 2 files changed +48
-0
lines changed Original file line number Diff line number Diff line change 1+ # -*- coding: utf-8 -*-
2+ class Solution_1_10 (object ):
3+ def convert (self , s : str , numRows : int ) -> str :
4+ """
5+ 6
6+ :param s:
7+ :param numRows:
8+ :return:
9+ """
10+ if numRows == 1 :
11+ return s
12+
13+ item = []
14+ arr = []
15+ i = 0
16+ while s :
17+ item .append (s [0 ])
18+ i += 1
19+ s = s [1 :]
20+ if i == 2 * numRows - 2 :
21+ arr .append (item )
22+ item = []
23+ i = 0
24+ if item :
25+ arr .append (item )
26+ final_res = []
27+ for row in range (numRows ):
28+ for idx in range (len (arr )):
29+ if len (arr [idx ]) < row + 1 :
30+ continue
31+ final_res .append (arr [idx ][row ])
32+ if 0 < row < numRows - 1 :
33+ if len (arr [idx ]) < 2 * numRows - 1 - row :
34+ continue
35+ final_res .append (arr [idx ][2 * numRows - 2 - row ])
36+ return '' .join (final_res )
Original file line number Diff line number Diff line change 1+ # -*- coding: utf-8 -*-
2+ from unittest import TestCase
3+ from sln_1_100 .solution_1_10 import Solution_1_10
4+
5+
6+ class Test_Solution_1_10 (TestCase ):
7+ def setUp (self ) -> None :
8+ self .sln = Solution_1_10 ()
9+
10+ def test_convert (self ):
11+ self .assertEqual (self .sln .convert ("PAYPALISHIRING" , 3 ), "PAHNAPLSIIGYIR" )
12+ self .assertEqual (self .sln .convert ("AB" , 1 ), "AB" )
You can’t perform that action at this time.
0 commit comments