1. 程式人生 > >leetcode problem 6: zigzag word

leetcode problem 6: zigzag word

append urn () lee public nbsp logs class turn

class Solution {
public:
    string convert(string s, int numRows) {
            if (numRows == 1){
                return s;
            }
            string out;
            for (int i = 0; i < numRows; ++i){
                int j = 0;
                while (true){
                    
int pos = -1; if (j % 2 == 0){ pos = j * (numRows - 1) + i; } else if (i != 0 && i != numRows - 1) { pos = j * (numRows - 1) + numRows - i - 1; }
if (pos >= (int)s.length()){ break; } if (pos >= 0){ out.append(1, s[pos]); } j ++; } } return out; } };

leetcode problem 6: zigzag word