[LeetCode]6. ZigZag Conversion(排序,暴力)
2017-03-14 20:46
162 查看
题目链接:https://leetcode.com/problems/zigzag-conversion/#/description
题意:把字符串摆成这种样子:
后面跟的numRows代表每竖列的字符个数。输出从左上到右下顺序的字符串。
暴力,维护坐标和字符,扔到vector里,排序就行了额。
题意:把字符串摆成这种样子:
后面跟的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 };
相关文章推荐
- leetcode 493. Reverse Pairs 归并排序统计逆序对数量 + 这个我估计是做不出来的,还是直接暴力吧
- 未排序寻找连续 leetcode -- Longest Consecutive Sequence
- HDU 4020 Ads Proposal(排序,暴力)
- LeetCode 之排序 sorting
- Leetcode:Reorder List 单链表重排序
- 【翻自LeetCode】求两排序后数组的中位数
- [leetcode] Merge Sorted Array 直接插入排序法解题
- LeetCode Sort List(链表排序)
- [leetcode]单链表插入排序
- leetcode刷题6:对单链表重新排序--reorder list
- poj 1054 The Troublesome Frog 排序后暴力枚举,剪枝优化
- Snowflake Snow Snowflakes 根据相似度排序,有点暴力
- Sort List 链表排序@LeetCode
- LeetCode:strStr 暴力美学
- Next Permutation 求下一个排序@LeetCode
- [LeetCode] 从排序的单链表到平衡搜索二叉树
- hdu 1394 求循环串的最小逆序数 暴力法 线段树 归并排序3种方法
- Codeforces_358A_Dima and Continuous Line(排序+暴力)
- LeetCode(Remove Duplicates from Sorted List) 两道删除排序链表中重复数字
- Leetcode 暴力 Generate Parentheses