6. Zigzag Conversion

C++ | Easy Under Standing | Brute Force | Vector


 #include <bits/stdc++.h>

using namespace std;


class Solution

{

public:

    string convert(string s, int numRows)

    {

        if (numRows == 1)

        {

            return s;

        }

        vector<string> rows(min(numRows, int(s.size())));

        int curRow = 0;

        bool goingDown = false;

        for (char c : s)

        {

            rows[curRow] += c;

            if (curRow == 0 || curRow == numRows - 1)

            {

                goingDown = !goingDown;

            }

            curRow += goingDown ? 1 : -1;

        }

        string ans;

        for (string row : rows)

        {

            ans += row;

        }

        return ans;

    }

};

Comments

Popular posts from this blog

1431. Kids With the Greatest Number of Candies

125. Valid Palindrome

771. Jewels and Stones