LeetCode(6) ZigZag Conversion
2024-09-30 02:53:15
题目
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;
}
};
最新文章
- 时间服务器:NTP 服务器
- 虚拟化平台cloudstack(2)——安装(上)
- 对Xcode菜单选项的详细探索(干货)
- tomcat7的安装与配置、及Servlet部署
- H5+app前端后台ajax交互总结
- .Net IE10 _doPostBack 未定义
- 使用 Docker 搭建 Java Web 运行环境
- poj 3281 Dining【拆点网络流】
- Zlib文件压缩和解压
- PHPRPC for PHP
- python 学习源码练习(2)——简单文件读取
- 昂贵的聘礼 POJ - 1062(最短路)
- Java反射讲解
- cf932E. Team Work(第二类斯特灵数 组合数)
- [Win] Win8权限机制导致R安装包失败
- Debian-Linux配置网卡网络方法
- Centos7与Windows10添加Windows10启动项并设置为默认启动
- url中含有%
- chrome插件开发,易懂
- ballerina 学习二十八 快速grpc 服务开发
热门文章
- 2-SAT问题(白书)
- 洛谷 P2056 [ZJOI2007]捉迷藏 || bzoj 1095: [ZJOI2007]Hide 捉迷藏 || 洛谷 P4115 Qtree4 || SP2666 QTREE4 - Query on a tree IV
- 转 11g RAC R2 体系结构---Grid
- 每天学点linux命令之locate 与 find 命令
- 213 House Robber II 打家劫舍 II
- javascript 笔记--变量
- MySQL-时间(time、date、datetime、timestamp和year)
- Unity笔记(4)自学第四、五天
- leetcode_Stone Game_dp_思维
- 数据结构之线性顺序表ArrayList(Java实现)