Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions arai60/zigzag_conversion/phase1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
def create_string_index_to_store_index(numRows: int)->dict:
string_index_to_store_index = {}
for index in range(numRows):
string_index_to_store_index[index] = index
#{0: 0, 1: 1, 2: 2, ..., numRows - 1: numRows - 1}
for index in range(numRows-2):
string_index_to_store_index[index + numRows] = numRows - index - 2
# 周期2*numRows - 2で変化するindexの辞書ができた
return string_index_to_store_index

def string_index(index, numRows):
if numRows == 1:
return 0
return index % (2 * numRows - 2)

class Solution:
def convert(self, s: str, numRows: int) -> str:
concat_strings = ["" for _ in range(numRows)]
string_index_to_store_index = create_string_index_to_store_index(numRows)
for index, string in enumerate(s):
concat_strings[string_index_to_store_index[string_index(index, numRows)]] += string
concated_string = ""
for string in concat_strings:
concated_string += string
return concated_string
36 changes: 36 additions & 0 deletions arai60/zigzag_conversion/phase2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# discordで他の人の回答も見てみた。
# 結構な人が文字列をガチャガチャしていていた
# https://github.com/nittoco/leetcode/pull/4/files, https://github.com/hayashi-ay/leetcode/pull/71/files
# 僕も最初思いついた解法は, 1行目は周期ずつ文字列を飛ばす, 2行目は周期ずつ文字列を飛ばし,
# 何個か戻ったところに文字があるのでそれを回収してくる...のような方法でいこうとしたが, 頭がこんがらがってしまい, 他にいい方法がないかを考えてみた。
# i行目にある文字は表現するためには, 文字列のj-index番目とnumRowsに関する何らかの余りで表現できることに気づいたので, それを用いた。

def create_string_index_to_column_index(numRows: int)->dict:
string_index_to_column_index = {}
for string_index in range(numRows):
column_index = string_index
string_index_to_column_index[string_index] = column_index
#{0: 0, 1: 1, 2: 2, ..., numRows - 1: numRows - 1}
for difference_index in range(numRows - 2):
string_index = numRows + difference_index
column_index = numRows - difference_index - 2
string_index_to_column_index[string_index] = column_index
# 周期2*numRows - 2で変化するindexの辞書ができた
#{0: 0, 1: 1, 2: 2, ..., numRows - 1: numRows - 1, numRows: numRows - 2, ..., 2 * numRows - 3: 1}
return string_index_to_column_index

def create_remain_string_index(index, numRows):
if numRows == 1:
return 0
return index % (2 * numRows - 2)

class Solution:
def convert(self, s: str, numRows: int) -> str:
concat_strings = ["" for _ in range(numRows)]
string_index_to_column_index = create_string_index_to_column_index(numRows)
for string_index, string in enumerate(s):
concat_strings[string_index_to_column_index[create_remain_string_index(string_index, numRows)]] += string
concated_string = ""
for string in concat_strings:
concated_string += string
return concated_string
28 changes: 28 additions & 0 deletions arai60/zigzag_conversion/phase3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
def create_string_index_to_column_index(numRows: int)->dict:
string_index_to_column_index = {}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List のほうがよくないですか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これは今回の問題的に辞書のkeyとなるところが数字なので配列で指定する方がわかりやすいからでしょうか, それとも余りを格納するなら配列だなあという感じでしょうか?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

append できるし、番号が飛んでいない事が分かるし、あたりですかね。

for string_index in range(numRows):
column_index = string_index
string_index_to_column_index[string_index] = column_index
for difference_index in range(numRows-2):
string_index = numRows + difference_index
column_index = numRows - difference_index - 2
string_index_to_column_index[string_index] = column_index
# return dict: {0:0, 1:1, ..., numRows-1: numRows-1, numRows: numRows-2, ..., 2*numRows-3:1}
return string_index_to_column_index
Copy link
Copy Markdown

@liquo-rice liquo-rice Apr 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

やはり、column_indexではなく、row_indexでしょうか?
このようでどうでしょう。

row_indices = [0, 1, ..., numRows - 1, numRows - 2, ..., 1]
for i, c in enumerate(s):
  row_index = row_indices[i % len(row_indices)]
  rows[row_index].append(c)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C++だとこんな感じですかね。

class Solution {
public:
    string convert(string s, int numRows) {
        vector<int> row_indices;
        for (int i = 0; i < numRows; ++i) {
            row_indices.push_back(i);
        }
        for (int i = numRows - 2; i > 0; --i) {
            row_indices.push_back(i);
        }

        vector<string> rows(numRows);
        for (int i = 0; i < s.size(); ++i) {
            int row_index = row_indices[i % row_indices.size()];
            rows[row_index].push_back(s[i]);
        }
        
        ostringstream ss;
        for (const auto& row : rows) {
            ss << row;
        }
        return ss.str();
    }
};

他にはこの解法が良さそうです。shining-ai/leetcode#60


def create_remain_string_index(string_index, numRows):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

名前だけで何をする関数かは分かりませんでした。

if numRows == 1:
return 0
return string_index % (2 * numRows - 2)

class Solution:
def convert(self, s: str, numRows: int) -> str:
concat_strings = ["" for _ in range(numRows)] #Store the strings of each column
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

concat_stringsというのは後でconcatするというのは伝わるのですが、concat_strings[i]には、何が入っているかは分かりかねました。Row iのstringでしょうか?

コメントの#の後にスペースがあったりなかったりが気になります。スペースあった方がいいでしょう。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Row iのstringのつもりでおりました。確かに数日経って見直すとよくわかんないですねこれ

string_index_to_column_index = create_string_index_to_column_index(numRows) #dict: string_index->column_index
for string_index, string in enumerate(s):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string_index -> istring -> charまたはcでどうでしょう。

column_index = string_index_to_column_index[create_remain_string_index(string_index, numRows)]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

column_indexじゃなくて、row_indexではないんでしょうか。

concat_strings[column_index] += string
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

文字列の += は基本は再構築なので、場合によっては遅いことは一応頭に入れておいて欲しいです。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

了解です。配列にためておいて最後に.joinした方がいいでしょうか

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あ, "".join(list)のことです

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そうですね。CPython だと最適化効くこともあるみたいなんですが。

concated_string = ""
for string in concat_strings:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

joinでできそうです。

concated_string += string
return concated_string
30 changes: 30 additions & 0 deletions arai60/zigzag_conversion/phase4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
Reference
shining-ai san: https://github.com/shining-ai/leetcode/pull/60/files
周期を考えて, 何番目のrowにいれるかという解法をstep1-3から書いてきたが,
shining-aiのコードを見てrowの移動方向を変える方が頭のリソースの節約にも,
空間計算量の節約にもつながることに気づいたので使ってみます


"""

class Solution:
def convert(self, s: str, numRows: int) -> str:
if numRows == 1:
return s

row_strings = [[] for _ in range(numRows)]

direction = 1
row_index = 0
for c in s:
row_strings[row_index].append(c)

if row_index == 0:
direction = 1
elif row_index == numRows - 1:
direction = -1

row_index += direction

return "".join(row_char for row_string in row_strings for row_char in row_string)