Leetcode#6 ZigZag Conversion

原题地址

 

找规律题

 

代码:

 1 string convert(string s, int nRows) {

 2         string res;

 3         

 4         if (nRows <= 1)

 5             return s;

 6         

 7         for (int r = 0; r < nRows; r++) {

 8             int curr = r;

 9             int prev = -1;

10             bool toggle = true;

11             while (curr < s.length()) {

12                 if (prev != curr)

13                     res += s[curr];

14                 prev = curr;

15                 curr += toggle ? 2 * (nRows - r - 1) : 2 * r;

16                 toggle = !toggle;

17             }

18         }

19         

20         return res;

21 }

 

你可能感兴趣的:(conversion)