-
Notifications
You must be signed in to change notification settings - Fork 0
Zigzag conversion #4
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
base: arai60
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
| 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 |
| 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 = {} | ||
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. やはり、column_indexではなく、row_indexでしょうか? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. concat_stringsというのは後でconcatするというのは伝わるのですが、concat_strings[i]には、何が入っているかは分かりかねました。Row iのstringでしょうか? コメントの
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| column_index = string_index_to_column_index[create_remain_string_index(string_index, numRows)] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. column_indexじゃなくて、row_indexではないんでしょうか。 |
||
| concat_strings[column_index] += string | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 文字列の += は基本は再構築なので、場合によっては遅いことは一応頭に入れておいて欲しいです。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 了解です。配列にためておいて最後に.joinした方がいいでしょうか
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. あ, "".join(list)のことです There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. そうですね。CPython だと最適化効くこともあるみたいなんですが。 |
||
| concated_string = "" | ||
| for string in concat_strings: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. joinでできそうです。 |
||
| concated_string += string | ||
| return concated_string | ||
| 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) |
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.
List のほうがよくないですか?
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.
これは今回の問題的に辞書のkeyとなるところが数字なので配列で指定する方がわかりやすいからでしょうか, それとも余りを格納するなら配列だなあという感じでしょうか?
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.
append できるし、番号が飛んでいない事が分かるし、あたりですかね。