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. (转)CentOS/Debian/Ubuntu系统 TCP-BBR 一键安装脚本

    本脚本适用环境 系统支持:CentOS 6+,Debian 7+,Ubuntu 12+ 虚拟技术:OpenVZ 以外的(KVM.Xen.VMware等) 内存要求:≥128M 日期 :2017 年 0 ...

  2. 实验吧-web-Guess Next Session(session简介)

    看代码: <?php session_start(); if (isset ($_GET['password'])) { if ($_GET['password'] == $_SESSION[' ...

  3. 安卓Recycleview简单的网格布局-初学者的关键点

    导包 def supportVersion = '28.0.0' 定义常量我的sdk版本为28 implementation "com.android.support:recyclervie ...

  4. H5页面单点登录跳回首页 http url参数转义

    在往首页跳的时候因为是单点登录进来的,url后面会带有参数,然后存入会话,所以我要拿到原本存入会话的参数放入url后面 但是返回的时候页面报错了 http://localhost:18086/h5ap ...

  5. duilib之重写BUTTON按钮

    在使用BUTTON过程中,有时候发现一些属性不够用,或要从新绘制BUTTON按钮,那该如何操作?其实很简单,只需要继承CButtonUI类就行. 创建类CMyButtonUI,继承CButtonUI, ...

  6. 《Java并发编程的艺术》并发编程的基础(四)

    一.线程简介 1.线程的概念 系统运行的最小单元 2.为何使用多线程 更好地利用系统资源(处理器多核心),提高响应速度. 3.线程的状态 NEW(创建状态) RUNABLE(运行状态,系统调度,争抢时 ...

  7. asp.net mvc邮箱激活

    1.发送邮件 public ActionResult SendEmail() { var member = dbSession.MemberRepository.LoadEntities(p => ...

  8. Ubuntu跨版本安装软件

    更新到Ubuntu 19.10之后,源里的Goldendict就会不时的崩溃,让我十分心累.过了这么长时间也一直没有更新,估计在20.04之前是不会更新了.这段时间因为疫情不能出门,正好看看这个问题, ...

  9. C#文本操作

    1.使用FileStream读写文件 文件头: 复制代码代码如下: using System;using System.Collections.Generic;using System.Text;us ...

  10. 一、thinkphp安装

    参考:https://www.kancloud.cn/manual/thinkphp5/118006 0.在此之前安装好phpstudy https://www.xp.cn 1.下载composer, ...