코테준비 - Zigzag Conversion

정상화·2023년 2월 26일

LeetCode

목록 보기
6/222

Zigzag Conversion

class Solution {
public:
    std::string convert(std::string s, int numRows) {
        int totalJump = (numRows - 1) * 2;
        int strLen = s.length();
        if(strLen == 1 || numRows == 1) return s;

        std::string converted = "";

        for (int i = 0; i < numRows; i++) {
            int first = totalJump - i * 2;
            int second = i * 2;
            bool isFirst = false;
            for (int j = i;
                 j < strLen; j += (first == 0 || second == 0) ? std::max(first, second) : isFirst ? first : second) {
                converted += s.at(j);
                isFirst = !isFirst;
            }
        }

        return converted;
    }
};
profile
백엔드 희망

0개의 댓글