Zigzag convert
public static String Convert(String s,int row) {
char[] c=s.toCharArray();
int len=s.length();
StringBuilder[] stringBuilders=new StringBuilder[row];
for (int i = 0; i < row; i++) {
stringBuilders[i]=new StringBuilder();
}
int i=0;
while(i<len){
for (int j = 0; j < row&&i<len; j++) {
stringBuilders[j].append(c[i++]);
}
for (int j =row-2; j >=1; j--) {
stringBuilders[j].append(c[i++]);
}
}
for (int j = 1; j < stringBuilders.length; j++) {
stringBuilders[0].append(stringBuilders[j]);
}
return stringBuilders[0].toString();
}
/*思路:
1. 首先是按行输出,每行输出相同位置的下一间隔为row*2-2,行游标0<i<row;
2. 行内输出,考虑中间斜向部分,首行和末行,直接打印空格,中间部分,需要打印斜向字符及前后空格
斜向部分字符在字符串中的坐标为span+j-2*i,其中j为行内游标(列游标)
斜向字符前面空格数为row-2-i,斜向字符后空格数为i-1
3. 为什么是span+j-2*i?
初始化时(每一行的首个斜向部分),j=i,字符位置为span-i+(j-i),其中j-i==0;
也必须这样初始化,因为要打印出首列的字符,通过j=i初始化,完成j的自增1,还能控制本行下一字符的输出;
第二个斜向部分,j+=span,字符位置为span*2-i==span-i+(j+=span)-i==span+j-2*i;
....
重点是:span-i是位置,配合行内游标j,使得位置可以行内自增,但是要减去初始化的值,即j=i;
所以,有了span-i+j-i,即span+j-2*i*/
public static void printZigZag(String s,int row) {
if (s.length()==0||row<1) {
return;
}
if (row==1) {
System.out.println(s);;
}
int span=row*2-2;
int zig=row-2;
//控制行打印
for (int i = 0; i < row; i++) {
//控制每行,垂直列上的输出,间隔为span
for (int j = i; j < s.length(); j+=span) {
System.out.print(s.charAt(j));
//控制中间斜向部分的输出,其中斜向输出字符在字符串中的位置为span+j-2*i
if (i!=0&&i!=row-1&&span+j-2*i<s.length()) {
for (int j2 = 0; j2 < zig-i; j2++) {
System.out.print(" ");
}
System.out.print(s.charAt(span+j-2*i));
for (int j2 = 0; j2 < i-1; j2++) {
System.out.print(" ");
}
}
else {
for (int j2 = 0; j2 < zig; j2++) {
System.out.print(" ");
}
}
}
System.out.println();
}
}
Zigzag convert的更多相关文章
- [LeetCode] ZigZag Converesion 之字型转换字符串
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 ...
- No.006:ZigZag Conversion
问题: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- ZigZag Conversion leetcode java
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- 【leetcode❤python】 6. ZigZag Conversion
#-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object): def convert(self, s, numRow ...
- LeetCode 6 ZigZag Conversion 模拟 难度:0
https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in ...
- leetcode6:Zigzag Conversion@Python
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- leetcode 6. ZigZag Conversion
https://leetcode.com/problems/zigzag-conversion/ 题目: 将字符串转化成zigzag模式. 例如 "abcdefghijkmlnpq" ...
- [LeetCode]ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
随机推荐
- Increasing Triplet Subsequence
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- linux pep8 检查工具
感谢dongweiming大神.
- VIM基础
http://www.cnblogs.com/wawlian/archive/2012/05/22/2512801.html http://www.cnblogs.com/wawlian/archiv ...
- NGUI 滑动页(UIToggle和UIToggledObjects)
1.NGUI->Create->Scroll View 2.给Scroll View添加一个 UIGrid,自己设置Arragement(横向竖向) 3.给Grid添加元素 4.给元素添加 ...
- Linux基础命令总结
1.pwd 查看当前工作目录 2.ls [目录] 列出指定目录下的所有文件,使用 ls -l 或者 ll 列出文件详细列表包括权限.大小等文件默认大小以字节B为单位,目录大小为4096B ls - ...
- CentOS搭建svn服务器支持https访问
在CentOS6.3 64位机器上配置SVN服务器,并设置只允许HTTPS连接,可以配置多个repos源,每个源都拥有自己的组和成员,用于权限控制. 安装相关软件 Apache yum install ...
- Python: 程序print到文件中
Python 3.x 将输出内容写入到一个文件,需要两个命令.open指明Python用什么文件名,w 意味着我们要写入该文件, encoding=”utf-8″指明Python如何把中文写入该文件. ...
- codeforces 515A.Drazil and Date 解题报告
题目链接:http://codeforces.com/problemset/problem/515/A 题目意思:问能否从 (0, 0) 出发,恰好走 s 步,到达该位置(a, b). 首先容易知道, ...
- C/S love自编程序
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- LightOJ1336 Sigma Function(约数和为偶数的个数)
Sigma Function Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit ...