6. ZigZag Conversion

  • Total Accepted: 98584
  • Total Submissions: 398018
  • Difficulty: Easy

  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".

 
  思路:
  首先,我们需要弄清楚这个ZigZag是怎么排列的,这个单词直译过来就是折线、之字形的,在这里不好具体表达,直接放一张图在下面,大家感受一下就知道了。下面分别是行数为5行、8行、10行的情况下的打印图
 
        

 
  通过上面的图我们知道,这种排列是有周期的,一个“V”字形为一个周期,我们知道除了第一行和最后一行没有重叠,其他行都有重叠,所以周期为period = 2*nRows-2。所以,我们打印的时候可以之间根据这个周期来进行按行打印。
  我们假设设定的共有n行,
  • 显然,n = 1 时,转换之后的字符串最后就是原先的字符串了
  • 当n > 1时
    • 那么第 0 行,没有重复,所有字符的位置index就是0,0+period,0+2*period。。。即每个周期内添加一个字符
    • 对于第 1 行,显然有重复,所有字符的位置index就是(1,0 +period - 1 ),(1+period,0+2*period-1),(1+2*peroid,0+3*period-1).。。。即每个周期内添加两个字符
    • 。。。
    • 对于第 i 行,显然有重复,所有字符的位置index就是(i,0 +period - i ),(i+period,0+2*period-i),(i+2*peroid,0+3*period-i).。。。即每个周期内添加两个字符
    • 。。。
    • 对于第 (n-1) 行,也就是最后一行,没有重复,所有字符的位置index就是(n-1), (n-1)+ period  , (n-1)+2* period 。。。即每个周期内添加一个字符
    • 对于第0行和最后一行有一个特点就是 (period-i)%peroid == i

  所以,代码如下:

 public String convert(String s, int numRows) {
if(s == null || s.length() <= numRows || numRows == 1){
return s ;
}
StringBuilder res = new StringBuilder() ;
char [] arr = s.toCharArray() ;
int period = 2*numRows - 2 ;
for(int i = 0 ; i < numRows ; i++){
for(int j = i ; j < s.length(); j += period){
if((period-i)%period != i){
res.append(arr[j]) ;
if((j+period-2*i) < s.length()){
res.append(arr[j+period-2*i]) ;
}
}else{
res.append(arr[j]) ;
}
}
} return res.toString() ;
}
 

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

  1. 【LeetCode】006. ZigZag Conversion

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

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

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

  3. No.006 ZigZag Conversion

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

  4. leetcode题解 6.ZigZag Conversion

    6.ZigZag Conversion 题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a gi ...

  5. 【JAVA、C++】LeetCode 006 ZigZag Conversion

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

  6. [Leetcode]006. ZigZag Conversion

    public class Solution { public String convert(String s, int nRows) { if (s == null || s.isEmpty() || ...

  7. 【一天一道LeetCode】#6 ZigZag Conversion

    一天一道LeetCode系列 (一)题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given ...

  8. 006 ZigZag Conversion

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

  9. 【LeetCode】6. ZigZag Conversion Z 字形变换

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字形变换,ZigZag,题解,Leetcode, 力扣,P ...

随机推荐

  1. zabbix监控内存占前3位的进程信息

    一.编写shell脚本 ps aux|sort -k4nr|head -3|awk 'split($11,a,"/"){print $4","a[length( ...

  2. CSS3 white-space属性

    white-space 属性设置如何处理元素内的空白. 可能的值 值 描述 normal 默认.空白会被浏览器忽略. pre 空白会被浏览器保留.其行为方式类似 HTML 中的 <pre> ...

  3. 小强学渲染之OpenGL的CPU管线

    读到这里,应该对OpenGL渲染管线有了初步简单了解.下面着重分析CPU管线,即逻辑控制中心做了什么,这部分还是容易理解的.如下图: 一,将数据加载到显存中. 这是由GPU是访问显存中的数据决定的.因 ...

  4. f5 主备模式切换

    f5 主备模式  主机down自动切换到备  原主机重新启动,自动切换到原主机

  5. FortiGate抓包 Sniffer

    1.图形界面抓包 系统管理--网络--数据包捕获 选择添加好的数据捕获,点击"运行"开关抓包:抓取包后,可以点击"下载"将抓取的数据包保存的本地磁盘,可以用wi ...

  6. eclipse中集成python开发环境

    转载:https://www.cnblogs.com/mywood/p/7272487.html Eclipse简介 Eclipse是java开发最常用的IDE,功能强大,可以在MAC和Windos上 ...

  7. centos 7 添加中文输入法

    中文输入法

  8. ArrayList 初探

    1.ArrayList继承AbstractList,实现List.RandomAccess.Cloneable.Serializable接口 public class ArrayList<E&g ...

  9. 201621123002《JAVA程序设计》第二周学习总结

    1.本周学习总结 1.重点String类 2.Java的数据类型 3.Java中的引用类,包装类 for(类型 元素变量名(任取):遍历对象(数组名)) 2.书面作业 1.String-使用Eclip ...

  10. nc 画界面,触发效果(第一种)

    package nc.ui.hzctr.sellctr.action; import java.awt.BorderLayout; import java.awt.Dimension; import ...