/**
* Source : https://oj.leetcode.com/problems/zigzag-conversion/
*
* Created by lverpeng on 2017/6/29.
*
*
* 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".
*
*/
public class ZigzagConvertion { /**
* 将给定的字符串转换为指定行数的锯齿状,然后按行输出
* 找出规律:
* 第一行和最后一行,数组下标相差2 * nRows - 2
* 中间的行:数组下表相差2 * nRows - 2 - 2 * i,i表示第i行
* 要注意下标越界判断
*
* @param text
* @param nRows
* @return
*/
public String convert (String text, int nRows) {
int n = text.length();
int base = 2 * nRows- 2;
int index = 0; if (nRows == 1) {
System.out.println(text);
} String out = "";
for (int i = 0; i < nRows; i++) {
index = i;
if (i == 0 || i == nRows - 1) {
while (index < n) {
out += text.charAt(index);
index += base;
}
} else {
while (index < n) {
out += text.charAt(index);
index += base - 2 * i;
}
}
}
return out;
} /**
* 实现准备好nRows的数组,遍历字符串,依次判断字符落在那一行,也就是哪一个数组
*
* @param text
* @param nRows
* @return
*/
public String convertByIndex (String text, int nRows) {
if(text.length() <= 1 || text.length() == nRows) {
return text;
}
String[] lines = new String[nRows];
int row = 0;
int step = 0;
for (int i = 0; i < text.length(); i++) {
if (row == 0) {
// row需要增加
step = 1;
}
if (row == nRows - 1) {
// row需要往回退
step = -1;
}
lines[row] = lines[row] == null ? "" : lines[row];
lines[row] += text.charAt(i);
row += step;
} String out = "";
for (String line : lines) {
out += line;
}
return out;
} public static void main(String[] args) {
ZigzagConvertion zigzagConvertion = new ZigzagConvertion();
System.out.println(zigzagConvertion.convert("PAYPALISHIRING", 3));
System.out.println(zigzagConvertion.convertByIndex("PAYPALISHIRING", 3));
} }

leetcode — zigzag-conversion的更多相关文章

  1. 6.[leetcode] ZigZag Conversion

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

  2. LeetCode ZigZag Conversion(将字符串排成z字型)

    class Solution { public: string convert(string s, int nRows) { string a=""; int len=s.leng ...

  3. LeetCode: ZigZag Conversion 解题报告

    ZigZag ConversionThe string "PAYPALISHIRING" is written in a zigzag pattern on a given num ...

  4. [leetcode]ZigZag Conversion @ Python

    原题地址:https://oj.leetcode.com/problems/zigzag-conversion/ 题意: The string "PAYPALISHIRING" i ...

  5. [LeetCode]ZigZag Conversion

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

  6. LeetCode——ZigZag Conversion

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

  7. [LeetCode] ZigZag Conversion [9]

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

  8. C++ leetcode::ZigZag Conversion

    mmp,写完没保存,又得重新写.晚上写了简历,感觉身体被掏空,大学两年半所经历的事,一张A4纸都写不满,真是一事无成呢.这操蛋的生活到底想对我这个小猫咪做什么. 今后要做一个早起的好宝宝~晚起就诅咒自 ...

  9. Leetcode:ZigZag Conversion分析和实现

    问题的大意就是将字符串中的字符按锯齿状(倒N形)垂直由上向下放置,最后水平从左向右读取.比如 ABCDEFGHIJKLMN,4表示 A          G      M B      F  H    ...

  10. LeetCode解题报告—— 2 Keys Keyboard & Longest Palindromic Substring & ZigZag Conversion

    1. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...

随机推荐

  1. centos7 smplayer 安装 安装视频播放器

    # yum -y install http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noar ...

  2. Python下安装MySQLdb模块

    ----------------------[针对Windows下python 的MySQLdb模块安装]--------------------- 一.检查MySQLdb模块是否安装,可在DOS命令 ...

  3. php一些简单的作业题

  4. java web+模板

    这次测试需要在java web的基础上套入模板,在测试的过程中我遇到了许多问题,现在我可以使用模板来美化网页的许多格式.但是模板的许多代码我还是看不懂,其中有jquery的许多代码.在今后我会学习相关 ...

  5. kbmmw 5.07 正式发布

    来了来了 5.07.00 Dec 9 2018           Important notes (changes that may break existing code)         === ...

  6. POJ1862 Stripies 贪心 B

    POJ 1862 Stripies https://vjudge.net/problem/POJ-1862 题目:     Our chemical biologists have invented ...

  7. 2019.03.01 bzoj2555: SubString(sam+lct)

    传送门 题意简述: 要求在线支持两个操作 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了几次?(作为连续子串) 思路: 考虑用lctlctlct来动态维护samsa ...

  8. hdu-1878(欧拉回路)

    题目链接:传送门 思路:就是判断无向图的欧拉回路的两个条件:(1)连通性(2)点的度数是偶数 注意:两个条件一同时满足才行. #include<iostream> #include< ...

  9. JavaScript基础视频教程总结(011-020章)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  10. MFC头文件

    AFX.H struct CRuntimeClass; // object type information class CObject; // the root of all objects cla ...