6. ZigZag Conversion

官方的链接:6. ZigZag Conversion

Description :

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

问题描述

Z型转换输出转换后的字符串

思路

方法一、参考官网
这也是比较容易理解的,建立numRows个字符串,然后遍历原字符串,对数组正数和倒数,逐个把字符按照规则添加到这些字符串数组中,最后合并。

方法二、不建立数组,直接根据规则一行一行拼接字符串。
1、Z型走法,一组总共是n = numRows + numRows - 2,即n个字符
2、从第0行开始计数,记为第row行,第0行和最后一行numRows-1的规则比较好确认:n * j + row即为第row所有的字符,j从0开始,直到n * j + row临界,row行共有j个字符
3、其他行的字符,除了上面的记录字符,在不会越界的情况下,还会多一个连接的字符,这个字符的下标可以这样计算:(j + 1) * n - row,其实(j + 1) * n是下一组的开头,减去当前的行数row,即可得到下一个字符,比如上面的例子,row=1,j=0,下一组的字符是A(第一行的第2个字符)(如下图),计算出来的下标3,即P(第2行第2个字符)(如下图),合并。

[github-here]

 public class Q6_ZigZagConversion {
public String convert(String s, int numRows) {
if (numRows == 1) {
return s;
}
int n = numRows + numRows - 2, len = s.length();
StringBuilder result = new StringBuilder();
for (int row = 0; row < numRows; row++) {
int j = 0, headIndex = j * n + row, tailIndex = (j + 1) * n - row;
while (headIndex < len) {
result.append(s.charAt(headIndex));
j++;
headIndex = j * n + row;
if (row != 0 && row != numRows - 1 && tailIndex < len) {
result.append(s.charAt(tailIndex));
tailIndex = (j + 1) * n - row;
}
}
}
return result.toString();
} public static void main(String[] args) {
Q6_ZigZagConversion s = new Q6_ZigZagConversion();
System.out.println(s.convert("PAYPALISHIRING", 3));
}
}

Q6:ZigZag Conversion的更多相关文章

  1. No.006:ZigZag Conversion

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

  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. 6. ZigZag Conversion

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

  6. 64. ZigZag Conversion

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

  7. LeetCode 6 ZigZag Conversion(规律)

    题目来源:https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is writt ...

  8. No.006 ZigZag Conversion

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

  9. leetcode第六题 ZigZag Conversion (java)

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

随机推荐

  1. java文件的上传

    文件的上传和下载在web应用中是非常常用,也是非常有用的功能.  例如:发送电子邮件时可以同过上传附件发送文件,OA系统中可以通过上传文件来提交公文,社交网站通过上传图片来自定义头像等等.  例如:下 ...

  2. tools.windows.bat#批处理

    关于%~ %~I - 删除任何引号("),扩展 %I %~fI - 将 %I 扩展到一个完全合格的路径名 %~dI - 仅将 %I 扩展到一个驱动器号 %~pI - 仅将 %I 扩展到一个路 ...

  3. pytesseract 识别率低提升方法

    pytesseract 识别率低提升方法 一.跟换识别语言包 下载地址https://github.com/tesseract-ocr/tessdata 二.修改图片的灰度 from PIL impo ...

  4. 利用python模拟鼠标点击自动完成工作,提升你的工作效率!

    没有什么能比学以致用让学习变得更有动力的了. 不知道大家在工作中有没有一些工作需要重复的点击鼠标,因为会影响到财务统计报表的关系,我们每个月底月初都要修改ERP中的单据日期,单据多的时候光修改就能让你 ...

  5. B. Email from Polycarp

    B. Email from Polycarp time limit per test 3 seconds memory limit per test 256 megabytes input stand ...

  6. 九九乘法表的四种三角形排布方式(for循环以及while循环的互换)

    #region //右上 for (int i = 1; i <= 9; i++){ for (int j = 1; j <= 9; j++){ if (i > j){ Consol ...

  7. 软件包管理:RPM包管理-yum在线管理

    CentOS 是免费的的 RedHat需要付费 1.IP地址配置 setup  #使用setup工具 (这种方式配置的永久有效 同时还可以配置掩码 网关等) 直接输入setup就会弹出(注意的是该命令 ...

  8. win下的常用8个命令

    windows下常用的几个指令 一,ping 它是用来检查网络是否通畅或者网络连接速度的命令.作为一个生活在网络上的管理员或者黑客来说,ping命令是第一个必须掌握的DOS命令,它所利用的原理是这样的 ...

  9. Golang的常量定义及使用案例

    Golang的常量定义及使用案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.常量的定义 package main import ( "fmt" ) fu ...

  10. CAN分帧发送程序说明

    试验平台 仅仅 需要一台主机 一台 周立功 CAN 助手, 一个232 助手就OK ICAN 协议 资源节点地址 电脑 我认为是0x01 51单片机主机的地址 是 0x1f 建立连接的 功能码 是0x ...