-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
20 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
Sep 13, 2024 | ||
|
||
* re-solve "6. Zigzag Conversion" | ||
|
||
Sep 12, 2024 | ||
|
||
* re-solve "14. Longest Common Prefix" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,28 @@ | ||
class Solution: | ||
def convert(self, s: str, num_rows: int) -> str: | ||
if num_rows <= 1: | ||
if num_rows == 1: | ||
return s | ||
|
||
result = [] | ||
n = len(s) | ||
result: list[str] = [] | ||
|
||
first, second = num_rows * 2 - 2, 0 | ||
|
||
start = 0 | ||
while start < num_rows and start < len(s): | ||
i = start | ||
step = (num_rows - 1) * 2 | ||
|
||
# first row | ||
for i in range(0, n, step): | ||
result.append(s[i]) | ||
|
||
while True: | ||
i += first | ||
|
||
if first and i < len(s): | ||
result.append(s[i]) | ||
|
||
i += second | ||
# middle rows | ||
for r in range(1, num_rows - 1): | ||
for i in range(r, n, step): | ||
result.append(s[i]) | ||
|
||
if second and i < len(s): | ||
result.append(s[i]) | ||
j = i + (step - r * 2) | ||
if j < n: | ||
result.append(s[j]) | ||
|
||
if i >= len(s): | ||
break | ||
|
||
start += 1 | ||
first -= 2 | ||
second += 2 | ||
# last row | ||
for i in range(num_rows - 1, n, step): | ||
result.append(s[i]) | ||
|
||
return "".join(result) |