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. Day6 - 牛客102C

    链接:https://ac.nowcoder.com/acm/contest/102/C来源:牛客网 题目描述  We define a value of an interval is the seco ...

  2. 004.CI4框架CodeIgniter, 配置mysql数据库,并进行数据库查询

    01.在app的Config目录的Database文件里面填写数据库配置,并把pConnect设置成true,此处为一直连接mysql数据库 02.在Models中,创建一个System目录,再在Sy ...

  3. SpringBoot-集成通用mapper

    SpringBoot-集成通用mapper SpringBoot-集成通用mapper ​ 我们在SpringBoot中整合了MyBatis,但是大量重复的增删改查还是很头疼的问题,MyBatis也给 ...

  4. Unity 脚本中的update,fixedupdate,lateupdate

    先放着 有功儿夫再来整理 https://www.cnblogs.com/fly-100/p/3777731.html https://www.cnblogs.com/hont/p/5184802.h ...

  5. HDU - 6205 card card card (尺取法)

    题意:有n堆牌,ai表示每堆牌的牌数,bi表示每堆牌的penaltyvalue,操作开始前,可以重复进行将第一堆牌挪到最后一堆这一操作.然后,对于挪完后的牌,从第一堆开始,依次取.对于每一堆牌,首先将 ...

  6. TX2开发板Ubuntu16.04设置静态IP

    TX2开发板Ubuntu16.04设置静态IP  https://www.cnblogs.com/qilai/p/11285445.html 首先打开一个Terminal输入 ifconfig 查看自 ...

  7. Mybatis 向MySql数据库插入带有日期类型字段的数据

    我们的实体类里面一个字段的日期类型是util.Date,在向数据库插入该实体时会报错,说是 日期哪个字段 Data truncation.所以需要做些更改在mybatis的MAPPER映射文件中对插入 ...

  8. 《动手学深度学习》系列笔记—— 1.2 Softmax回归与分类模型

    目录 softmax的基本概念 交叉熵损失函数 模型训练和预测 获取Fashion-MNIST训练集和读取数据 get dataset softmax从零开始的实现 获取训练集数据和测试集数据 模型参 ...

  9. torch.cuda.FloatTensor

    Pytorch中的tensor又包括CPU上的数据类型和GPU上的数据类型,一般GPU上的Tensor是CPU上的Tensor加cuda()函数得到. 一般系统默认是torch.FloatTensor ...

  10. 逆向-PE重定位表

    重定位表 ​ 当链接器生成一个PE文件时,会假设这个文件在执行时被装载到默认的基地址处(基地址+RVA就是VA).并把code和data的相关地址写入PE文件.如果像EXE一样首先加载就是它image ...