美文网首页
[Leetcode] 6. ZigZag Conversion

[Leetcode] 6. ZigZag Conversion

作者: Kevifunau | 来源:发表于2018-10-02 15:56 被阅读0次

    解题思路 :
    之字型打印
    蓝色为 垂直向下 x++
    绿色为 东北方向 x--, y ++


    image.png
    /*
    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
    
    P   A   H   N
    A P L S I I G
    Y   I   R
    And then read line by line: "PAHNAPLSIIGYIR"
    
    Write the code that will take a string and make this conversion given a number of rows:
    
    string convert(string s, int numRows);
    Example 1:
    
    Input: s = "PAYPALISHIRING", numRows = 3
    Output: "PAHNAPLSIIGYIR"
    Example 2:
    
    Input: s = "PAYPALISHIRING", numRows = 4
    Output: "PINALSIGYAHRPI"
    Explanation:
    
    P     I    N
    A   L S  I G
    Y A   H R
    P     I
    */
    
    #include <iostream>
    #include <string>
    #include <algorithm>
    
    using namespace std;
    
    class Solution
    {
      public:
        string convert(string s, int numRows)
        {
    
            if (s == "" || numRows == 0)
            {
                return "";
            }
    
            if (numRows == 1)
            {
                return s;
            }
    
            const int row = numRows;
            const int col = s.size();
            char mat[row][col];
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    mat[i][j] = '0';
                }
            }
            
            int count = 0;
            int i = 0;
            int j = 0;
    
            while (count < s.size())
            {
    
                for (int x = 0; x < row && count < s.size(); x++)
                {
                    mat[x][j] = s[count++];
                }
    
                for (int x = row - 2; x >= 1 && count < s.size(); x--)
                {
                    mat[x][++j] = s[count++];
                }
                j++;
            }
    
    
    
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    cout << mat[i][j] << ' ';
                }
                cout << '\n';
            }
            string ans = "";
    
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    if (mat[i][j] != '0')
                    {
                        ans += mat[i][j];
                    }
                }
            }
    
            return ans;
        }
    };
    
    int main()
    {
        Solution s;
        string str = "ABCDEFGHIGKSDJKJS";
        int nums = 4;
        cout << s.convert(str, nums) << endl;
        return 0;
    }
    

    输出:

    kevifunaudeMacBook-Pro:leetcode fmb$ ./a.out
    A 0 0 G 0 0 D 0 0 0 0 0 0 0 0 0 0
    B 0 F H 0 S J 0 0 0 0 0 0 0 0 0 0
    C E 0 I K 0 K S 0 0 0 0 0 0 0 0 0
    D 0 0 G 0 0 J 0 0 0 0 0 0 0 0 0 0
    AGDBFHSJCEIKKSDGJ
    

    然而速度很慢 -_-


    image.png

    应该 可以把矩阵优化一下,懒 不想动。。

    相关文章

      网友评论

          本文标题:[Leetcode] 6. ZigZag Conversion

          本文链接:https://www.haomeiwen.com/subject/dqcaoftx.html