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 ...
随机推荐
- linearlayout 中ImageView 居中等问题
linearlayout 下的子控件使用android:layout_gravity=”center” 控件居左,没有达到居中的效果, 父窗体只能指定一种控件摆放方向 横向还是竖向 下面我弄了三个 ...
- BZOJ4377 Kurs szybkiego czytania \ Luogu 3589[POI2015]KUR - 数学思维题
Solution 我又双叒叕去看题解啦$QAQ$, 真的想不到鸭 输入 $a$ 和 $n$ 互质, 所以满足 $a \times i \ mod \ n$ $(0<=i<n)$ 肯定是不重 ...
- linux python 安装到用户目录
在公司服务器中,python可能存在多个版本,而且python中的包也有多个不同版本,由于不同猿的需求不同,经常会引起程序冲突,影响工作效率.因此,给大家分享一个在没有root权限时,将python安 ...
- MySQL中校验规则(collation)的选取对实际数据筛选的影响
在mysql中存在着各种utf8编码格式,如下表:1)utf8_bin2)utf8_general_ci utf8_bin将字符串中的每一个字符用二进制数据存储,区分大小写.utf8_genera_c ...
- C#情怀与未来
C#情怀与未来,怨天尤人还是抓住机会,能否跟上dnc新时代浪潮? 经常看到有.NET圈子在讨论是否应该转其它语言 C#情怀是一方面,如果觉得C#未来没前途,光靠情怀是撑不住的, 建议对C#未来 ...
- EBS CAS SSO测试
https://wiki.jasig.org/display/CAS/CASifying+Oracle+Portal https://wenku.baidu.com/view/5f110a85b9d5 ...
- java 项目的路径详情
title: 项目下的路径问题tags:grammar_cjkRuby: true--- 在javaee的项目中,存取文件,解析xml和properties文件,以及项目中的文件,都需要获取路径,常用 ...
- Codeforces gym101612 E.Equal Numbers(贪心)
传送:http://codeforces.com/gym/101612 题意:给出一个大小为n的序列a[i],每次选其中一个数乘以一个正整数,问进行k步操作后最少剩下多少种数字,输出0≤k≤n,所有的 ...
- windows快速打开命令窗口方式[利刃篇]
windows当然是窗口界面操作了,谁有事没事去用什么命令行啊,但是当你要用的时候,也要会用才行哦. 打开命令行的方式小说一下: 1.开始 > 运行 > cmd , enter, ok ...
- php cli模式和浏览器访问下加载php.ini文件的注意事项[架构篇]
使用wampserver或Xampp时,会将配置文件放在一个统一的目录中去调用,这时如果都使用浏览器访问,自然是没有问题的,但是如果换成cli命令行模式运行,则会出现加载了的扩展无法使用的问题. 案例 ...