标签:
The string "PAYPALISHIRING"
is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
思路:
间距=2*numRows-2;
class Solution { public: string convert(string s, int numRows) { string ans; int len=s.length(); if(numRows==1||len<=numRows) return s;//处理特殊情况 for(int i=0;i<numRows;i++){ int interval=2*(numRows-i)-2,j=i,flag=1;; ans.push_back(s[i]); while(j<len){ if(flag){ i==0?flag=1:flag=0; i<numRows-1?j=j+interval:j=j+2*numRows-2; if(j<len) ans.push_back(s[j]); } else{ flag=1; i<numRows-1?j=j+2*i:j=j+2*numRows-2; if(j<len) ans.push_back(s[j]); } } } return ans; } };
标签:
原文地址:http://www.cnblogs.com/wqkant/p/5285670.html