Conversation
|
2*numRows-1で割ったあまり昇順の順番にでてくることを利用して何回かfor文を回すと空間計算量O(1)でかける...? |
| @@ -0,0 +1,28 @@ | |||
| def create_string_index_to_column_index(numRows: int)->dict: | |||
| string_index_to_column_index = {} | |||
There was a problem hiding this comment.
これは今回の問題的に辞書のkeyとなるところが数字なので配列で指定する方がわかりやすいからでしょうか, それとも余りを格納するなら配列だなあという感じでしょうか?
| string_index_to_column_index = create_string_index_to_column_index(numRows) #dict: string_index->column_index | ||
| for string_index, string in enumerate(s): | ||
| column_index = string_index_to_column_index[create_remain_string_index(string_index, numRows)] | ||
| concat_strings[column_index] += string |
There was a problem hiding this comment.
文字列の += は基本は再構築なので、場合によっては遅いことは一応頭に入れておいて欲しいです。
There was a problem hiding this comment.
了解です。配列にためておいて最後に.joinした方がいいでしょうか
There was a problem hiding this comment.
あ, "".join(list)のことです
|
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.
concat_stringsというのは後でconcatするというのは伝わるのですが、concat_strings[i]には、何が入っているかは分かりかねました。Row iのstringでしょうか?
コメントの#の後にスペースがあったりなかったりが気になります。スペースあった方がいいでしょう。
There was a problem hiding this comment.
Row iのstringのつもりでおりました。確かに数日経って見直すとよくわかんないですねこれ
| def convert(self, s: str, numRows: int) -> str: | ||
| concat_strings = ["" for _ in range(numRows)] #Store the strings of each column | ||
| 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.
string_index -> i、string -> charまたはcでどうでしょう。
| concat_strings = ["" for _ in range(numRows)] #Store the strings of each column | ||
| string_index_to_column_index = create_string_index_to_column_index(numRows) #dict: string_index->column_index | ||
| for string_index, string in enumerate(s): | ||
| column_index = string_index_to_column_index[create_remain_string_index(string_index, numRows)] |
There was a problem hiding this comment.
column_indexじゃなくて、row_indexではないんでしょうか。
| # return dict: {0:0, 1:1, ..., numRows-1: numRows-1, numRows: numRows-2, ..., 2*numRows-3:1} | ||
| return string_index_to_column_index | ||
|
|
||
| def create_remain_string_index(string_index, numRows): |
| 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.
やはり、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)
There was a problem hiding this comment.
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
| column_index = string_index_to_column_index[create_remain_string_index(string_index, numRows)] | ||
| concat_strings[column_index] += string | ||
| concated_string = "" | ||
| for string in concat_strings: |
問題
https://leetcode.com/problems/zigzag-conversion/?envType=list&envId=xo2bgr0r
周期性に注目するのが大事だと考えた