leetcode — zigzag-conversion
/**
* 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的更多相关文章
- 6.[leetcode] ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- LeetCode ZigZag Conversion(将字符串排成z字型)
class Solution { public: string convert(string s, int nRows) { string a=""; int len=s.leng ...
- LeetCode: ZigZag Conversion 解题报告
ZigZag ConversionThe string "PAYPALISHIRING" is written in a zigzag pattern on a given num ...
- [leetcode]ZigZag Conversion @ Python
原题地址:https://oj.leetcode.com/problems/zigzag-conversion/ 题意: The string "PAYPALISHIRING" i ...
- [LeetCode]ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- LeetCode——ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- [LeetCode] ZigZag Conversion [9]
称号 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- C++ leetcode::ZigZag Conversion
mmp,写完没保存,又得重新写.晚上写了简历,感觉身体被掏空,大学两年半所经历的事,一张A4纸都写不满,真是一事无成呢.这操蛋的生活到底想对我这个小猫咪做什么. 今后要做一个早起的好宝宝~晚起就诅咒自 ...
- Leetcode:ZigZag Conversion分析和实现
问题的大意就是将字符串中的字符按锯齿状(倒N形)垂直由上向下放置,最后水平从左向右读取.比如 ABCDEFGHIJKLMN,4表示 A G M B F H ...
- 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 ...
随机推荐
- Python:每日一题005
题目: 输入三个整数x,y,z,请把这三个数由小到大输出. 程序分析: 我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,然后再用x与z进行比较,如果x> ...
- HQL数据查询基础
HQL定义 1.Hibernate Query Language, Hibernate查询语言 2.HQL是面向对象的查询语言(HQL查询的主体是映射配置的持久化类及其属性而SQL查询主体是数据库表) ...
- 【转】linux 查看进程启动路径
在linux下查看进程大家都会想到用 ps -ef|grep XXX可是看到的不是全路径,怎么看全路径呢?每个进程启动之后在 /proc下面有一个于pid对应的路径例如:ps -ef|grep pyt ...
- JAVA 8 主要新特性 ----------------(一)总纲
一.版本中数据结构的修改浅析 二.JDK1.8优点概括 三.新功能Lambda表达式入门 四.Lambda函数式接口 五.Lambda方法引用与构造器引用 六.集合Stream API 七.新时间日期 ...
- 分享《机器学习实战基于Scikit-Learn和TensorFlow》中英文PDF源代码+《深度学习之TensorFlow入门原理与进阶实战》PDF+源代码
下载:https://pan.baidu.com/s/1qKaDd9PSUUGbBQNB3tkDzw <机器学习实战:基于Scikit-Learn和TensorFlow>高清中文版PDF+ ...
- requests应用
一.简介 什么是requests模块: requests模块是python中原生的基于网络请求的模块,其主要作用是用来模拟浏览器发起请求.功能强大,用法简洁高效.在爬虫领域中占据着半壁江山的地位. 为 ...
- maya2012卸载/安装失败/如何彻底卸载清除干净maya2012注册表和文件的方法
maya2012提示安装未完成,某些产品无法安装该怎样解决呢?一些朋友在win7或者win10系统下安装maya2012失败提示maya2012安装未完成,某些产品无法安装,也有时候想重新安装maya ...
- Kubernetes-1
master 节点负责管理整个集群,管理的控制面板,全局的角色和调度 3个组件 API Server : 统一入口 kubectl 客户端管理工具 Etcd 数据库 Scheduler 集群的调度 C ...
- 1,rocketmq 的原理与安装教程
参考文档 http://blog.csdn.net/a19881029/article/details/34446629 https://github.com/alibaba/RocketMQ htt ...
- Linux下解压.tar.xz格式文件的方法
前言 对于xz这个压缩相信很多人陌生,但xz是绝大数linux默认就带的一个压缩工具,xz格式比7z还要小. 今天在下载Node.js源码包的时候遇到的这种压缩格式.查了一下资料,这里进行一下记录,分 ...