電源ボタンのブラッシングに関する質問:6、ジグザグ変換

主題の要件

ここに画像の説明を挿入

  • 1 <= s.length <= 1000
  • sは、英字(小文字と大文字)、「、」、および「。」で構成されます。
  • 1 <= numRows <= 1000

オリジナルタイトルリンク

コード

class Solution {
    
    
public:
    string convert(string s, int numRows) {
    
    
        if (numRows == 1)
        {
    
    
            return s;
        }
        int interval = numRows - 2;
        vector<string> strV(numRows);
        for (size_t i = 0; i < s.length(); i++)
        {
    
    
            int index = i % (numRows + interval);
            if (index < numRows)
            {
    
    
                strV[index] += s[i];
            }
            else
            {
    
    
                index = index - numRows;
                strV[strV.size() - index - 2] += s[i];
            }
        }
        for (auto i : strV)
            cout << i << ends;
        string resultStr;
        for (auto i : strV)
            resultStr += i;
        return resultStr;
        
    }
};

全体のアイデア

この質問で最も重要なことは、文字列とバリアントパターンを観察することです。パターンのパターンを見つけた後、残りは解決されます。
文字列と行番号が表示されます。
文字列内の文字はグループに分けられます。各グループには、numRows +(numRows-2)文字があります。
各グループで、最初のnumRowsは、対応する番号付きコンテナーに通常の順序で配置されます。次のnumRows-2文字は、2つの垂直ジグザグ間の対角線です。最後から2番目の行から前の行まで逆の順序で開始します。

学んだこと

1.文字列の処理...

おすすめ

転載: blog.csdn.net/youyadefeng1/article/details/113404345