算法练习6. ZigZag Conversion 把一串字符反N方向n行排列
2016-05-09 22:22
429 查看
6. ZigZag Conversion
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 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".
把一串字符反N方向n行排列:
class
Solution {
public:
string convert(string
s, int
numRows) {
if (numRows <= 1)return
s;
string result;
int i, j, k,len=s.length();
int inter =
numRows*2-2,step=0;
for (i = 0; i <
numRows; i++)
{
if (i!=0&&i !=numRows - 1)
{
step = inter - 2 * i;
j = i;
while (j < len)
{
result += s.at(j);
j = j + step;
step = inter - step;
}
}
else{
step = inter;
j = i;
while (j < len)
{
result += s.at(j);
j += step;
}
}
}
return result;
}
};
相关文章推荐
- 关于HTTP_CLIENT_IP,HTTP_X_FORWAR
- 10大白帽黑客专用的 Linux 操作系统
- 有关Mysql分库分表,读写分离数据库水平拆分
- iOS SDK开发时,有关OC和C++混编造成其它类错误的问题(Compile Sources As Objective-C++)
- 有return的情况下try catch finally的执行顺序
- 折半查找
- 给定数组,创建(带头节点)的单链表(头插法、尾插法)
- SQL SERVER 导入、导出数据到Exce(使用OpenRowset,、OpenDataSource函数)以及访问远程数据库(openrowset/opendatasource/openquery)
- VS2012 安装出错 :通道正在关闭
- poj 3164(最小树形图)
- HDU 1358 (KMP)
- java 内部类
- VS2015 create a C++ console application based on WinRT
- webstrom 中 plugins error 设置里 Languages & Frameworks里面没有JavaScript?
- 学完IT十八掌第1天java基础课程:
- VM和ESXi的一些参数
- sqlnet.ora
- 练习n的阶乘
- Citrix虚拟化实施部署中的一些注意点
- 2016江苏省赛总结