问题:

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. ASP.NET MVC 从零开始 - 请求处理

    这篇文章是从我的 github 博客 lxconan.github.io 导入的. 这是这个系列的第三篇了.前两篇文章请参见: ASP.NET MVC 从零开始 - Create and Run AS ...

  2. YY一下微信线下支付的场景

    在上一篇文章里面提到了 <跨行清算的实现原理>,这次来分析一下线下支付的场景和流程. 今天看到一篇文章:http://www.huxiu.com/article/23248/1.html? ...

  3. Attribute富文本使用方法

    ★★★Attribut富文本★★★ 在UITextView和UILable的使用中很多的时候会用到富文本. UITextView和UILable的区别在于: ★★★★UITextView 当文字大于一 ...

  4. iOS-多线程基础

    进程与线程: 1>   一个应用程序对应一个进程,一个进程帮助程序占据一块存储空间 2>   要想在进程中执行任务,就必须开启线程,一条线程就代表一个任务 3>   一个进程中允许开 ...

  5. 浅谈cssText

    给一个HTML元素设置css属性,如 var head= document.getElementById("head"); head.style.width = "200 ...

  6. 上传伪技术~很多人都以为判断了后缀,判断了ContentType,判断了头文件就真的安全了。是吗?

    今天群里有人聊图片上传,简单说下自己的经验(大牛勿喷) 0.如果你的方法里面是有指定路径的,记得一定要过滤../,比如你把 aa文件夹设置了权限,一些类似于exe,asp,php之类的文件不能执行,那 ...

  7. 移动web app开发必备 - Deferred 源码分析

    姊妹篇  移动web app开发必备 - 异步队列 Deferred 在分析Deferred之前我觉得还是有必要把老套的设计模式给搬出来,便于理解源码! 观察者模式 观察者模式( 又叫发布者-订阅者模 ...

  8. jQuery2.0.3源码分析系列(28) 元素大小

    最近的分析都是有点不温不火,基本都是基础的回顾了 今年博客的目标目前总的来说有2大块 JS版的设计模式,会用jQuery来诠释 JS版的数据结构,最近也一直在狠狠的学习中. HTML息息相关的的样式 ...

  9. Caffe学习笔记2--Ubuntu 14.04 64bit 安装Caffe(GPU版本)

    0.检查配置 1. VMWare上运行的Ubuntu,并不能支持真实的GPU(除了特定版本的VMWare和特定的GPU,要求条件严格,所以我在VMWare上搭建好了Caffe环境后,又重新在Windo ...

  10. 深入学习jQuery事件对象

    × 目录 [1]获取 [2]事件类型 [3]事件目标[4]当前元素[5]事件冒泡[6]默认行为[7]命名空间[8]返回值[9]键值 前面的话 在触发DOM上的某个事件时,会产生一个事件对象event, ...