题目链接:https://leetcode.com/problems/zigzag-conversion/#/description
题意:把字符串摆成这种样子:
后面跟的numRows代表每竖列的字符个数。输出从左上到右下顺序的字符串。
暴力,维护坐标和字符,扔到vector里,排序就行了额。
1 typedef struct Node { 2 pair<int, int> p; 3 char c; 4 Node() {} 5 Node(pair<int, int> pp, char cc) : p(pp), c(cc) {} 6 }Node; 7 8 bool cmp(Node a, Node b) { 9 return a.p < b.p; 10 } 11 12 class Solution { 13 public: 14 vector<Node> G; 15 string convert(string s, int numRows) { 16 if(numRows > s.length() || numRows == 1) return s; 17 G.clear(); 18 int x = 0, y = 0; 19 int xf = 0; 20 string ret = ""; 21 for(int i = 0; i < s.length(); i++) { 22 if(x == numRows - 1) xf = 1; 23 if(x == 0) xf = 0; 24 G.push_back(Node(pair<int, int>(x, y), s[i])); 25 if(xf) x--, y++; 26 else x++; 27 } 28 sort(G.begin(), G.end(), cmp); 29 for(int i = 0; i < G.size(); i++) ret += G[i].c; 30 return ret; 31 } 32 };