Z字形变换 leetcode

class Solution {
public:
     string convert(string s, int numRows) {
        int len = s.size();
        string ans;
        if (numRows <= 1)  return s;
        for (int i = 0 ; i < numRows; i++) {
            int c1 = 2*numRows - 2*(i+1);
            int c2 = 2*i;
            int cnt = i;

            ans += s[cnt];
            while(cnt < len) {
                if (c1 != 0) {
                    cnt += c1;
                    if (cnt < len) {
                      ans +=  s[cnt];
                    }
                }
                if (c2 != 0) {
                    cnt += c2;
                    if (cnt < len) {
                      ans += s[cnt];
                    }
                }

            }
        }
        return ans;
    }
};

你可能感兴趣的:(leetcode)