https://leetcode.com/problems/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".

class Solution {
public:
string convert(string s, int numRows) {
if(numRows == 1)return s;
string* a = new string[numRows];
string ans;
int n = 2 * numRows - 2;
for(int i = 0;i < s.size();i++)
{
int r = i % n;
int l = r % numRows;
if(l == r)a[l] += s[i];
else a[numRows - l - 2] += s[i];
}
for(int i = 0;i < numRows;i++){
ans += a[i];
}
return ans;
}
};

  

最新文章

  1. C++ 取得系统当前时间
  2. java 23 - 2 设计模式之单例模式
  3. ThinkPHP3.2对接开发支付宝即时到帐接口
  4. MYSQL分库分表和不停机更改表结构
  5. Win7 64位下sql server链接oracle的方法
  6. Core Java Volume I — 3.3. Data Types
  7. android Tab =viewpager+fragmnet
  8. 初步STL集装箱List
  9. QT不让windows休眠的方法
  10. thinkphp的model模型的设计经验总结
  11. 自己动手编写Maven的插件
  12. linux安装vmware tools 步骤
  13. UE4利用Save Game创建全局变量
  14. dump_stack的简单使用 【转】
  15. Eclipse的maven项目一直无故报错
  16. 二维数组与类的定义_DAY06
  17. Gerrit安装配置
  18. LeetCode517. Super Washing Machines
  19. golang(5)使用beego 开发 api server 和前端同学拆分开发,使用swagger
  20. 转:Entity FrameWork利用Database.SqlQuery&lt;T&gt;执行存储过程并返回参数

热门文章

  1. Go语言执行系统命令行命令(转)
  2. nginx安装方式
  3. CentOS 6.5 yum安装配置lnmp服务器(Nginx+PHP+MySQL)
  4. linux编程中接收主函数返回值以及错误码提示
  5. SublimeText配置NodeJS代码提示
  6. webpack和gulp的区别
  7. Asp.Net_&lt;%%&gt;模式常用语法
  8. 使用JS实现轮播图的效果
  9. iOS 动画组
  10. js制作简单的计算器