Skip to content

Zigzag conversion#4

Open
SuperHotDogCat wants to merge 5 commits intoarai60from
zigzag_conversion
Open

Zigzag conversion#4
SuperHotDogCat wants to merge 5 commits intoarai60from
zigzag_conversion

Conversation

@SuperHotDogCat
Copy link
Copy Markdown
Owner

問題

https://leetcode.com/problems/zigzag-conversion/?envType=list&envId=xo2bgr0r

周期性に注目するのが大事だと考えた

@SuperHotDogCat
Copy link
Copy Markdown
Owner Author

@SuperHotDogCat
Copy link
Copy Markdown
Owner Author

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 = {}
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 できるし、番号が飛んでいない事が分かるし、あたりですかね。

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
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 だと最適化効くこともあるみたいなんですが。

@oda
Copy link
Copy Markdown

oda commented Apr 16, 2024

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のつもりでおりました。確かに数日経って見直すとよくわかんないですねこれ

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):
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でどうでしょう。

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)]
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ではないんでしょうか。

# 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):
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 = 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

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:
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でできそうです。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants