-
Notifications
You must be signed in to change notification settings - Fork 0
Solved Arai60/6. Zigzag Conversion #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Satorien
wants to merge
1
commit into
main
Choose a base branch
from
solve/arai60_6
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+111
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| ## Step 1. Initial Solution | ||
|
|
||
| - よく観察すると下に行ってから上に戻ってくるだけとも見れる | ||
| - numRows分のリストを用意しておいて順番に入れていく | ||
| - 最後に順番に後ろにくっつけていけば行ける | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def convert(self, s: str, numRows: int) -> str: | ||
| if numRows == 1: | ||
| return s | ||
| rows = [[] for _ in range(numRows)] | ||
| descending = True | ||
| index = 0 | ||
| row_index = 0 | ||
| while index < len(s): | ||
| if descending: | ||
| if row_index == numRows: | ||
| descending = False | ||
| row_index -= 2 | ||
| continue | ||
| rows[row_index].append(s[index]) | ||
| index += 1 | ||
| row_index += 1 | ||
| else: | ||
| if row_index == 0: | ||
| descending = True | ||
| continue | ||
| rows[row_index].append(s[index]) | ||
| index += 1 | ||
| row_index -= 1 | ||
|
|
||
| zigzag_string = '' | ||
| for row in rows: | ||
| zigzag_string += ''.join(row) | ||
| return zigzag_string | ||
| ``` | ||
|
|
||
| ### Complexity Analysis | ||
|
|
||
| - 時間計算量:O(n) | ||
| - 空間計算量:O(n) | ||
|
|
||
| ## Step 2. Alternatives | ||
|
|
||
| - もう少し上手いやり方がありそうだがあまり思いつかない | ||
| - https://github.com/tokuhirat/LeetCode/pull/60/files#diff-b770a1954146bd595658dc8a1791db788c438ab24d3b1727ec015a8991696d6dR70 | ||
| - なるほど、かなり綺麗に書けている | ||
| - directionを1,-1で定義してそれを足していくという発想は面白い | ||
| - ループの最後で行番号を増減させてから方向性の判定を行うと処理が見やすい | ||
| - "".join(c for row in row_to_letters for c in row) | ||
| - 外側にかかってgenerateできるということらしい | ||
| - 挙動を理解できていなかったので参考になった | ||
| - https://github.com/shintaro1993/arai60/pull/64/files#diff-7982c09acadc7e7c23e49111f0d36acc3994024cd122f55bebfefc94ca581bb2R140-R144 | ||
| - 一往復を一サイクルとみなして追加していく方法 | ||
|
|
||
| ```python | ||
| cycle = 2 * (numRows - 1) | ||
| for i, c in enumerate(s): | ||
| position = i % cycle | ||
| row_index = min(position, cycle - position) | ||
| rows[row_index].append(c) | ||
| ``` | ||
|
|
||
| - シンプルめに変形 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def convert(self, s: str, numRows: int) -> str: | ||
| if numRows == 1: | ||
| return s | ||
|
|
||
| row_to_letters = ['' for _ in range(numRows)] | ||
| descending = True | ||
| row = 0 | ||
| for letter in s: | ||
| row_to_letters[row] += letter | ||
| if descending: | ||
| row += 1 | ||
| if row + 1 == numRows: | ||
| descending = False | ||
| else: | ||
| row -= 1 | ||
| if row == 0: | ||
| descending = True | ||
| continue | ||
|
|
||
| return ''.join(row_to_letters) | ||
| ``` | ||
|
|
||
| ## Step 3. Final Solution | ||
|
|
||
| - directionを使ってみる実装 | ||
| - 理解が難しい訳ではないしこれで良さそう | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def convert(self, s: str, numRows: int) -> str: | ||
| if numRows == 1 or len(s) <= numRows: | ||
| return s | ||
|
|
||
| direction = 1 | ||
| row = 0 | ||
| row_in_letters = ['' for _ in range(numRows)] | ||
| for letter in s: | ||
| row_in_letters[row] += letter | ||
| row += direction | ||
| if row == 0 or row == numRows - 1: | ||
| direction *= -1 | ||
| return ''.join(row_in_letters) | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generator 使うのが気に入りましたが、ちょっと変わった方法かもしれませんね。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.6oum2lb2o7j4