问题:

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

  1. 首先考虑特殊情况,当nRows=1时,也存在“ZigZag”模式,就是原字符串本身。
  2. 因为最后得到的字符串是按照行来读取的,所以利用一个StringBuffer,将每一行的数据一个个的append进去。
  3. 不难发现,第一行和最后一行,是特殊的,其差值是一个定值2*(nRows-1)。
  4. 除去第一行和最后一行,其余行数,根据上行/下行的方向,每次的间隔值是不同的,但是每两次的间隔值之和是一个定值:2*(nRows-1)。
  5. 用一个flag的标志位,记录上行/下行方向,每在内循环中读取一个字符之后,改变flag的值,跑完一次内循环之后(即读完一行),将标志位还原。
  6. 入参检查。

解题代码:

 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/

https://github.com/Gerrard-Feng/LeetCode/blob/master/LeetCode/src/com/gerrard/algorithm/easy/Q006.java

PS:如有不正确或提高效率的方法,欢迎留言,谢谢!

No.006:ZigZag Conversion的更多相关文章

  1. Q6:ZigZag Conversion

    6. ZigZag Conversion 官方的链接:6. ZigZag Conversion Description : The string "PAYPALISHIRING"  ...

  2. leetcode:ZigZag Conversion 曲线转换

    Question: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ...

  3. LeetCode之“字符串”:ZigZag Conversion

    题目链接 题目要求: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ...

  4. LeetCode OJ:ZigZag Conversion(字符串的Z字型转换)

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  5. No.006 ZigZag Conversion

    6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...

  6. LeetCode--No.006 ZigZag Conversion

    6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...

  7. 《LeetBook》leetcode题解(6): ZigZag Conversion[E]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  8. 6. ZigZag Conversion

    题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  9. 64. ZigZag Conversion

    ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...

随机推荐

  1. 循序渐进,了解Hive是什么!

    一直想抽个时间整理下最近的所学,断断续续接触hive也有半个多月了,大体上了解了很多Hive相关的知识.那么,一般对陌生事物的认知都会经历下面几个阶段: 为什么会出现?解决了什么问题? 如何搭建?如何 ...

  2. Oracle 把秒转成时分秒格式(hh24:mm:ss);检测字符串是否是数字;字符串转换为数字

    不说废话,贴代码: CREATE OR REPLACE FUNCTION to_time(sec IN NUMBER) RETURN VARCHAR2 IS /*把秒转成时分秒格式 auth lzpo ...

  3. dede在php7上空白

    最近想看一本小说,想采集回来看,结果发现除了dedecms支持php7.0,其他主流cms基本上都不支持php7.0 在本地win7上调试了一遍,没有问题,放到linux服务器上的时候,发现打开任何页 ...

  4. java继承、抽象和接口

    package zdbExtends;public class Grandparent {        public Grandparent(){                System.out ...

  5. 《Qt Quick 4小时入门》学习笔记3

    http://edu.csdn.net/course/detail/1042/14807?auto_start=1 Qt Quick 4小时入门 第八章:Qt Quick中的锚(anchors)布局 ...

  6. VS2015签名丢失解决办法

    废话少说,直接上问题和解决办法, 问题:VS2015安装后运行程序会发现这个问题 解决办法 1.下载SignTool.exe签名文件 2.将签名文件放入 C:\Program Files (x86)\ ...

  7. 在.NET开发面向Oracle数据库的应用程序

    其实这个不是一个什么新的话题.但是之前在多次项目中,总是遇到大家针对Oracle数据库的访问时,会有各种各样的问题,最基本的就是要在客户端安装各种client,版本不一样的话还有各种问题. 静下心来看 ...

  8. ZooKeeper集群搭建中的Connection refused而导致的启动失败

    1. 前言 每一次搭建集群环境都像一次战斗,作战中任何一个细节的出错都会导致严重的后果,所以搭建中所需要做的配置如系统配置.网络配置(防火墙记得关).用户权限.文件权限还有配置文件等等内容,都必须非常 ...

  9. spring控制并发数的工具类ConcurrencyThrottleSupport和ConcurrencyThrottleInterceptor

    官方文档: /** * Support class for throttling concurrent access to a specific resource. * * <p>Desi ...

  10. Opencv VideoCapture实时捕捉摄像头信息

    #include "opencv2/highgui/highgui.hpp" #include <iostream> using namespace cv; using ...