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. android名词

    NDK:Native Development Kit JNI:Java Native Interface

  2. android中的简单animation(四)3D transition

    animation_main_screen.xml: <?xml version="1.0" encoding="utf-8"?> <Fram ...

  3. 使用jquery版本的viewer.js图片更新的问题

    参考博客: 使用jquery版本的viewer.js图片更新的问题 - cc_fys的博客 - CSDN博客 https://blog.csdn.net/cc_fys/article/details/ ...

  4. class(二)--派生类的继承

    前言 从我之前的一篇笔记对象的继承中, 我们可以知道JS的继承方式依赖原型链,而比较好的继承方式是寄生组合式继承 先来温习下什么是寄生组合式继承 function Rectangle(length, ...

  5. Spring AOP 管理事务

    <aop:config proxy-target-class="true"> <aop:pointcut expression="execution(* ...

  6. python中excel表格的读写

    #!usr/bin/env python #-*- coding:utf-8 -*- import xlrd import xlwt from xlutils.copy import copy imp ...

  7. pacificrack 控制面板登录不上的问题

    我今天又试了一下: https://master-stack01.pacificrack.com还是登不上(这个一键烦恼了我一个星期了,但是我今天百度出来了解决办法) 然后用这个就可以了  https ...

  8. 《ES6标准入门》(阮一峰)--11.对象的新增方法

    1.Object.is() ES5 比较两个值是否相等,只有两个运算符:相等运算符(==)和严格相等运算符(===).它们都有缺点,前者会自动转换数据类型,后者的NaN不等于自身,以及+0等于-0.J ...

  9. Golang的环境安装

    Golang的环境安装 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Go语言环境安装 1>.下载Golang的安装包 博主推荐阅读: https://studygola ...

  10. 解决Elasticsearch索引只读

    今天添加索引时发现kibana添加索引不生效,页面也没有报错,没有创建成功只是一闪而过. 另外发现各项目日志与当前时间差异很大,filebeat一直报错io timeout 具体报错如下: fileb ...