leetcode problem 6: zigzag word

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;
    }
};

 

posted @ 2017-09-10 23:15  nosaferyao  阅读(155)  评论(0编辑  收藏  举报