배움 저장소

[Leet Code] 006 ZigZag Conversion 본문

PS/LeetCode

[Leet Code] 006 ZigZag Conversion

시옷지읏 2021. 5. 10. 03:41
class Solution {
public:
    string convert(string s, int numRows) {
        if(numRows==1) return s;
        int divider = numRows + numRows-2;
        
        vector<string> matrix(numRows);
        
        for(int i=0; i<s.size(); ++i){
            int index = i % divider;
            if(index < numRows)
            {
                matrix.at(index%numRows).push_back(s[i]);
            } 
            else
            { 
                index = index % (numRows-1);
                matrix.at(numRows-1-index).push_back(s[i]);
            }
        }
        
        string result;
        for(string str:matrix){
            result.append(str);
            for(char c:str){
                cout << c << " ";
            }
            cout << endl;
        }
        return result;
    }
};

'PS > LeetCode' 카테고리의 다른 글

[Leet Code]055 Jump Game  (0) 2021.10.06
[Leet Code]009 Palindorme Number  (0) 2021.05.21
[Leet Code]007 Reverse Integer  (0) 2021.05.12
[LeetCode] 002 Add Two Numbers  (0) 2021.04.20
[LeetCode] 001 Two Sum  (0) 2021.04.20
Comments