/**
* 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. 综合评价模型C++实现

    1 综合评价模型建立步骤 综合评价模式是一种对一个或多个系统进行评价的模型.一般分为如下几个步骤: 选取评价指标,指标的选取应该具有独立性和全面性. 得到m×n测量矩阵,每一行表示一个带评价系统(共m ...

  2. CentOS 7 Redis 内网 安装 卸载

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

  3. java多线程系列15 设计模式 生产者 - 消费者模式

    生产者-消费者 生产者消费者模式是一个非常经典的多线程模式,比如我们用到的Mq就是其中一种具体实现 在该模式中 通常会有2类线程,消费者线程和生产者线程 生产者提交用户请求 消费者负责处理生产者提交的 ...

  4. java crach 日志解析

    在java开发中,或许会出现如下错误,这种错误大多出现在开发中涉及本地代码的地方. ## A fatal error has been detected by the Java Runtime Env ...

  5. Python Day 2

    阅读目录: 内容回顾   编程语言介绍 python语言介绍  安装官方cpython解释器 --版本共存  运行python代码   --交互式:实时交互   --脚本式:运行py文件的三步骤 变量 ...

  6. Python10/24--组合/封装/property装饰器/多态

    组合的应用: 1. 什么是组合 一个对象的属性是来自于另外一个类的对象,称之为组合 2. 为何用组合 组合也是用来解决类与类代码冗余的问题 3. 如何用组合 '''class Foo: aaa=111 ...

  7. 【pycharm 警告】unittest RuntimeWarning: Parent module ” not found while handling absolute import

    Pycharm 2016.2执行单元测试遇到如下问题: RuntimeWarning: Parent module ‘YOUR_MODULE_HERE’ not found while handlin ...

  8. 完整的SOPC开发流程体验

    课程目标:学习并掌握完整的SOPC开发流程. 开发环境:Quartus15.1 学习内容:1.使用QSYS工具建立能够运行流水灯项目的NIOS II处理器系统 2.在quartus ii中添加NIOS ...

  9. Navicat for MYSQL 断网时本地连接无法打开,2005错误

    Navicat for MYSQL 断网时本地连接无法打开,2005错误 NO1 提示下图: NO2 解决方法: (1)选中本地连接,右键 连接属性 (2) 将 主机名或IP地址 这一栏改为 127. ...

  10. Java 实现字符串的加密与解密

    package com.wangbo.util; import java.security.Key; import java.security.Security; import javax.crypt ...