题目

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)



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”.

分析

思路参考博客,在此表示对博主的感谢。

向下循环:nRows

斜角线循环:nRows-2(减去首尾两个端点)

重复

AC代码

class Solution {
public:
string convert(string s, int numRows) { if (s.empty() || numRows == 1)
return s;
//声明numRows个字符串,对该之字型序列处理
string *res = new string[numRows];
int i = 0, j, gap = numRows - 2;
while (i < s.size()){
for (j = 0; i < s.size() && j < numRows; ++j) res[j] += s[i++];
for (j = gap; i < s.size() && j > 0; --j) res[j] += s[i++];
}
string str = "";
for (i = 0; i < numRows; ++i)
str += res[i];
return str;
} };

GitHub测试程序源码

最新文章

  1. 时间服务器:NTP 服务器
  2. 虚拟化平台cloudstack(2)——安装(上)
  3. 对Xcode菜单选项的详细探索(干货)
  4. tomcat7的安装与配置、及Servlet部署
  5. H5+app前端后台ajax交互总结
  6. .Net IE10 _doPostBack 未定义
  7. 使用 Docker 搭建 Java Web 运行环境
  8. poj 3281 Dining【拆点网络流】
  9. Zlib文件压缩和解压
  10. PHPRPC for PHP
  11. python 学习源码练习(2)——简单文件读取
  12. 昂贵的聘礼 POJ - 1062(最短路)
  13. Java反射讲解
  14. cf932E. Team Work(第二类斯特灵数 组合数)
  15. [Win] Win8权限机制导致R安装包失败
  16. Debian-Linux配置网卡网络方法
  17. Centos7与Windows10添加Windows10启动项并设置为默认启动
  18. url中含有%
  19. chrome插件开发,易懂
  20. ballerina 学习二十八 快速grpc 服务开发

热门文章

  1. 2-SAT问题(白书)
  2. 洛谷 P2056 [ZJOI2007]捉迷藏 || bzoj 1095: [ZJOI2007]Hide 捉迷藏 || 洛谷 P4115 Qtree4 || SP2666 QTREE4 - Query on a tree IV
  3. 转 11g RAC R2 体系结构---Grid
  4. 每天学点linux命令之locate 与 find 命令
  5. 213 House Robber II 打家劫舍 II
  6. javascript 笔记--变量
  7. MySQL-时间(time、date、datetime、timestamp和year)
  8. Unity笔记(4)自学第四、五天
  9. leetcode_Stone Game_dp_思维
  10. 数据结构之线性顺序表ArrayList(Java实现)