ZigZag Conversion
2015-09-10 14:48
183 查看
The string
And then read line by line:
Write the code that will take a string and make this conversion given a number of rows:
"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 N A P L S I I G Y 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".
string convert(string s, int nRows) { if(nRows <= 1) return s; string result; if(s.size() ==0) return result; for(int i =0; i< nRows; i++) { for(int j =0, index =i; index < s.size(); j++, index = (2*nRows-2)*j +i) { result.append(1, s[index]); if(i ==0 || i == nRows-1) { continue; } if(index+(nRows- i-1)*2 < s.size()) { result.append(1, s[index+(nRows- i-1)*2]); } } } return result; }
相关文章推荐
- 使用PowerShell调用MTools分析MongoDB性能并发送邮件
- Raid 原理及创建软raid
- C# 队列Queue
- 两个链表的第一个公共结点
- Android中onMeasure方法详解
- VS2010折叠大纲快捷键
- android4.4 PowerManagerService流程分析
- [LeetCode 213] House Roober II
- 图形用户界面:通过ip获取地址并显示天气情况
- UIButton设置为圆形按钮并增加边框
- JAVA HashMap详细介绍和示例
- 学校类站群网站建设管理
- 获取状态栏高度
- Fragment和Activity
- 很简单的加密算法
- LSTM特点及适用性
- iPhone为什么不设置返回按钮
- lu用户体验_关闭按钮
- MongoDB— 分片技术
- java编程接口(1) ------ Swing基金会