No.006: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);
官方难度:
Easy
翻译:
现有字符串“PAYPALISHIRING”,写成n行“ZigZag”模式,结果为“PAHNAPLSIIGYIR”。
写出将一个字符串转译成“ZigZag”模式的代码。
额外例子:
字符串:“abcdefghijklmnopqrstuvwxyz”,nRows=5。
a i q y
b h j p r x z
c g k o s w
d f l n t v
e m u
“ZigZag”字符串:“aiqybhjprxzcgkoswdflntvemu”。
- 首先考虑特殊情况,当nRows=1时,也存在“ZigZag”模式,就是原字符串本身。
- 因为最后得到的字符串是按照行来读取的,所以利用一个StringBuffer,将每一行的数据一个个的append进去。
- 不难发现,第一行和最后一行,是特殊的,其差值是一个定值2*(nRows-1)。
- 除去第一行和最后一行,其余行数,根据上行/下行的方向,每次的间隔值是不同的,但是每两次的间隔值之和是一个定值:2*(nRows-1)。
- 用一个flag的标志位,记录上行/下行方向,每在内循环中读取一个字符之后,改变flag的值,跑完一次内循环之后(即读完一行),将标志位还原。
- 入参检查。
解题代码:
public static String convert(String s, int numRows) {
if (s == null || numRows <= 0) {
throw new IllegalArgumentException("Input error");
}
if (numRows == 1) {
return s;
}
char[] array = s.toCharArray();
StringBuffer buffer = new StringBuffer();
// 上行/下行标志位
int flag = 1;
// 间隔定值
int interval = 2 * (numRows - 1);
// 按行遍历
for (int i = 0; i < numRows; i++) {
// 每一行的数据
int j = 0;
while (i + j < array.length) {
buffer.append(array[i + j]);
// 第一行和最后一行
if (i == 0 || i == numRows - 1) {
j += interval;
} else {
if (flag == 1) {
// 上行
j += interval - 2 * i;
} else {
// 下行
j += 2 * i;
}
// 改变下次的标志位
flag *= -1;
}
}
// 结束一行的遍历,标志位还原
flag = 1;
}
return buffer.toString();
}
convert
相关链接:
https://leetcode.com/problems/zigzag-conversion/
PS:如有不正确或提高效率的方法,欢迎留言,谢谢!
No.006:ZigZag Conversion的更多相关文章
- Q6:ZigZag Conversion
6. ZigZag Conversion 官方的链接:6. ZigZag Conversion Description : The string "PAYPALISHIRING" ...
- leetcode:ZigZag Conversion 曲线转换
Question: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ...
- LeetCode之“字符串”:ZigZag Conversion
题目链接 题目要求: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ...
- LeetCode OJ:ZigZag Conversion(字符串的Z字型转换)
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- No.006 ZigZag Conversion
6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...
- LeetCode--No.006 ZigZag Conversion
6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...
- 《LeetBook》leetcode题解(6): ZigZag Conversion[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 6. ZigZag Conversion
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- 64. ZigZag Conversion
ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...
随机推荐
- [New Portal]Windows Azure Storage (14) 使用Azure Blob的PutBlock方法,实现文件的分块、离线上传
<Windows Azure Platform 系列文章目录> 相关内容 Windows Azure Platform (二十二) Windows Azure Storage Servic ...
- JavaScript css-dom
HTML负责结构层,网页的结构层由HTML或者XHTML之类的标记语言负责构建 CSS负责表示层,描述页面内容应该如何呈现. JavaScript负责行为层,负责内容应该如何响应事件这一问题. 能利用 ...
- Atitit 分区后的查询 mysql分区记录的流程与原理
Atitit 分区后的查询 mysql分区记录的流程与原理 1.1.1. ibd是MySQL数据文件.索引文件1 1.2. 已经又数据了,如何分区? 给已有的表加上分区 ]1 1.3. 分成4个区, ...
- ScriptTask读取SharePoint上的Excel
很多情况下,我们会SharePoint 作为文件共享的Server,有时,业务上需要将SharePoint上的Excel文档 Download 到db中.对于搞DB的人来说,难点是如何下载Excel? ...
- ASP.NET Core的配置(3): 将配置绑定为对象[上篇]
出于编程上的便利,我们通常不会直接利用ConfigurationBuilder创建的Configuration对象读取某个单一配置项的值,而是倾向于将一组相关的配置绑定为一个对象,我们将后者称为Opt ...
- 创建 VXLAN - 每天5分钟玩转 OpenStack(111)
前面我们讨论了 VXLAN 的理论知识,并且在 ML2 中完成了相关配置.今天将通过 Web UI 创建 vxlan100_net 并观察节点网络结构的变化. 打开菜单 Admin -> Net ...
- 在ASP.NET Web API项目中使用Hangfire实现后台任务处理
当前项目中有这样一个需求:由前端用户的一个操作,需要触发到不同设备的消息推送.由于推送这个具体功能,我们采用了第三方的服务.而这个服务调用有时候可能会有延时,为此,我们希望将消息推送与用户前端操作实现 ...
- 安装ClouderaManager以及使用ClouderaManager安装分布式集群的若干细节
目录 前言 整体介绍 分步安装介绍 总结 一.前言 周末干了近四十个小时中间只休息了五个小时终于成功安装了ClouderaManager以及分布式集群,其中各种辛酸无以言表,唯有泪两行. ...
- [翻译]利用顶点位移的VR畸变校正
文章英文原网址: http://www.gamasutra.com/blogs/BrianKehrer/20160125/264161/VR_Distortion_Correction_using_V ...
- Oracle Recovery 01 - 常规恢复之完全恢复
背景:这里提到的常规恢复指的是数据库有完备可用的RMAN物理备份. 实验环境:RHEL6.4 + Oracle 11.2.0.4 DG primary. 一.常规恢复之完全恢复:不丢失数据 1.1 单 ...