leetcode zigzag
来源:互联网 发布:轻而易举软件官网 编辑:程序博客网 时间:2024/06/18 18:03
题目要求:
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)
P A H NA P L S I I GY I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3)
should return "PAHNAPLSIIGYIR"
.
代码贴出!已AC。
class Solution {public: string convert(string s, int nRows) { if (nRows <= 1 || s.length() == 0) return s; int len = s.size(); string st[nRows]; int i = 0; while(i<len) { for (int idx = 0; idx < nRows && i < len; idx++) // vertically down st[idx] += (s[i++]); for (int idx = nRows-2; idx >= 1 && i < len; idx--) // obliquely up st[idx] += (s[i++]); } for (int idx = 1; idx < nRows; idx++) st[0].append(st[idx]); return st[0]; }};
0 0
- leetcode zigzag
- 【leetcode】zigzag
- LeetCode: ZigZag Conversion
- LeetCode : ZigZag Conversion
- leetcode 13:zigzag conversion
- [LeetCode] Zigzag Conversion
- LeetCode 6 - ZigZag Conversion
- Leetcode : ZigZag Conversion
- 【leetcode】Zigzag Conversion
- [LeetCode]ZigZag Conversion
- leetcode ZigZag Conversion
- LeetCode-ZigZag Conversion
- LeetCode ZigZag Conversion
- LeetCode 6: ZigZag Conversion
- leetcode-006:ZigZag Conversion
- Leetcode: ZigZag Conversion
- [LeetCode]ZigZag Conversion
- [LeetCode] ZigZag Conversion
- 汉诺塔VII
- svn学习
- 无法显示xml,根级别上的数据无效,第1行,位置1
- iOS 推送通知详解
- myeclipse2014 添加Feces 相关jar包不能自动导入复制在lib下
- leetcode zigzag
- poj 1328 【Radar Installation】【几何转化、区间覆盖】
- Setup DNS Server step by step in CentOS 6.3/6.4/6.5 / RHEL 6.3/6.4/6.5 / Scientific Linux 6.3/6.4/6.
- Xamarin.iOS设备标识OpenUDID(及UMID简介)
- Vitamio 是什么?Vitamio 新手入门
- 免费的pdf转换器注册码
- 百度主页里追加股票栏目
- java程序员要注意那些事项在面试中
- cout 输出字符串(指针)常见问题及put,write函数