/**
* 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. CentOS 7 Redis 内网 安装 卸载

    # 不能连接外网, 安装Redis服务器的过程 https://redis.io/download (官网下载安装包, 最新版) redis-*.tar.gz 放在安装目录/usr/local/red ...

  2. 查找 管道 exec

    #查找150天为使用的文件并列出find -type f -mtime +150 -exec ls -ltr {} \;#查找150天内120外的文件find -type f -mtime -150 ...

  3. solr7.7.0搜索引擎使用(二)(添加搜索)

    一.安装完毕之后,需要为solr添加core,每一个搜索server就是一个core,solr可以有很多core,我们需要创建一个core用于我们的搜索 添加core的方式有两种: 第一种进入solr ...

  4. 【Node.js】安装及使用

    Node.js是在Chrome的V8 JavaScript引擎上构建的JavaScript运行时.Node.js使用事件驱动的非阻塞I / O模型,使其轻量且高效.Node.js的软件包生态系统npm ...

  5. C#中读写INI文件

    C#中读写INI文件 c#的类没有直接提供对ini文件的操作支持,可以自己包装win api的WritePrivateProfileString和GetPrivateProfileString函数实现 ...

  6. Visual Studio2013 配置opencv3.3.0 x64系统

    注:小白一个,第一次写博客,可能会有一些理解上的错误,只此记录自己测试成功的坎坷之路,已备以后查看,同时给有需要之人. 我是win10 64 位,之前安装了visual studio 2013, 现在 ...

  7. [uboot] (番外篇)uboot之fdt介绍

    http://blog.csdn.net/ooonebook/article/details/53206623 以下例子都以project X项目tiny210(s5pv210平台,armv7架构)为 ...

  8. getObjectURL 上传图片预览

    js 函数 function getObjectURL(file) {    var url = null ;     if (window.createObjectURL!=undefined) { ...

  9. HTML5 添加新的标签 input属性

    <!-- 新增 有语意标签 --> <nav></nav> <!-- 导航标签 --> <seclion></seclion> ...

  10. python20分钟入门

    原子类型 # 内建函数名不可以做变量名如sum,ord abs(-1) # 1 d = dict() # d = {}空字典的构造 l = list() # l = [] s = set(list) ...