Leetcode#6 ZigZag Conversion
2015-06-24 22:13
429 查看
Difficulty EasyThe string
of rows like this: (you may want to display this pattern in a fixed font for better legibility)
And then read line by line:
return
"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"
class Solution {
public:
string convert(string s, int numRows) {int j = 0, len = s.length(),coun=0;
if(numRows==1||len==1)
return s;
string ans;
while(j<len)
{
ans =ans + s[j];
j = j + 2*numRows -2;}if(len>=2)
{
for(int i=1;i<=(numRows-2);i++)
{
int k =i;
while(k<len){
ans=ans + s[k];
k = k + 2*(numRows-1-i);if(k<len)
ans=ans + s[k];
else
break;
k = k + 2*i;}}}if(len>=numRows&&len!=1){
j = numRows - 1;
while(j<len)
{
ans=ans+ s [j];
j = j + 2*numRows -2;}
}cout<<ans<<endl;
return ans;
}};
相关文章推荐
- Android RecyclerView 使用详解
- Huffman编码用MTLAB的实现及编码注释----------Matlab
- 34.如何获取app(apk和ipa)中的资源
- servlet之cookied/session深入浅出(上)
- AspCms标签
- 34.如何获取app(apk和ipa)中的资源
- RulingSite-S 系统漏洞 (台湾大学居多)
- Maximum Depth of Binary Tree
- android学习笔记(六)——数据库操作
- CSS Sprites:鱼翅还是三鹿?
- get whole screen size;add background
- 在 Redis 上实现的分布式锁
- sendto 和 recvfrom 函数
- 第一~十七章的回答
- MTK android配置LCD背光和LED,调试方法
- 判断颜色是否相等UIColor
- Hive安装极其用法(个人觉得适合初学者)
- 单例模式
- Android 5.x Theme 与 ToolBar 详解
- PHP整合Jcrop截取的上传头像功能