LeetCode: ZigZag Conversion 解题报告
ZigZag Conversion
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".

SOLUTION 1:
使用以下算法会比较简单。
两个规律:
1 两个zigzag之间间距为2*nRows-2
2 每个zigzag中间(在j和j+interval之间)位置为j+interval-2*i
注意:当Rows = 1时,此方法不适用,因为size = 0,会造成死循环。所以Rows = 1时,需要独立处理。
引自:http://blog.csdn.net/fightforyourdream/article/details/16881517
public class Solution {
public String convert(String s, int nRows) {
if (s == null) {
return null;
}
// 第一个小部分的大小
int size = * nRows - ;
// 当行数为1的时候,不需要折叠。
if (nRows <= ) {
return s;
}
StringBuilder ret = new StringBuilder();
int len = s.length();
for (int i = ; i < nRows; i++) {
// j代表第几个BLOCK
for (int j = i; j < len; j += size) {
ret.append(s.charAt(j));
// 即不是第一行,也不是最后一行,还需要加上中间的节点
int mid = j + size - i * ;
if (i != && i != nRows - && mid < len) {
char c = s.charAt(mid);
ret.append(c);
}
}
}
return ret.toString();
}
}
2015.1.4 redo:
public class Solution {
public String convert(String s, int nRows) {
if (s == null) {
return null;
}
// corner case;
if (nRows == 1) {
return s;
}
// The number of elements in a section.
int section = 2 * nRows - 2;
StringBuilder sb = new StringBuilder();
int len = s.length();
for (int i = 0; i < nRows; i++) {
for (int j = i; j < len; j += section) {
char c = s.charAt(j);
sb.append(c);
// The middle rows.
int mid = j + section - 2 * i;
// bug 2: the mid is out of range.
if (i != 0 && i != nRows - 1 && mid < len) {
// bug 1: forget a ')'
sb.append(s.charAt(mid));
}
}
}
return sb.toString();
}
}
请至主页君的GIT HUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/string/Convert.java
LeetCode: ZigZag Conversion 解题报告的更多相关文章
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- 6.[leetcode] ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- LeetCode - Course Schedule 解题报告
以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...
- LeetCode ZigZag Conversion(将字符串排成z字型)
class Solution { public: string convert(string s, int nRows) { string a=""; int len=s.leng ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- 【LeetCode】281. Zigzag Iterator 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 deque 日期 题目地址:https://leetc ...
- [leetcode]ZigZag Conversion @ Python
原题地址:https://oj.leetcode.com/problems/zigzag-conversion/ 题意: The string "PAYPALISHIRING" i ...
- [LeetCode] ZigZag Conversion [9]
称号 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
随机推荐
- ORA-01109:数据库未打开(解决)
SQL> startup mountORA-01081: 无法启动已在运行的 ORACLE - 请首先关闭它SQL> shutdown immediateORA-01109: 数据库未打开 ...
- python之函数用法__str__()
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法__str__() #http://www.cnblogs.com/hongfei/p ...
- 4、第一个JAVA程序(Hello World)
第一步: 新建一个文本文档,在里面输入内容 public class HelloWorld { public static void main(String[] args){ System.out.p ...
- JVM heap中各generation的大小(Sizing the Generations)
查看参数 使用 -XX:+PrintFlagsFinal 打印当前环境JVM参数默认值, 比如: java -XX:PrintFlagsFinal -version, 也可以用java [生产环境参数 ...
- BeanUtils 装载java bean
Topic topic=new Topic(); Enumeration<String> enums=request.getParameterNames(); try { while(en ...
- java配置使用手册
进行java开发,首先要安装jdk,安装了jdk后还要进行环境变量配置: 1.下载jdk(http://java.sun.com/javase/downloads/index.jsp),我下载的版本是 ...
- log4j的NDC/MDC区别与应用
MDC与NDC除了存储方式(MDC采用MapNDC采用堆栈结构)有区别,其他都一样的 关键点 A -//引入log4j MDC类org.apache.log4j.MDC -//设置值 -MDC.put ...
- 在Windows Service上安装运行Redis
CSDN下载RedisWatcher,运行InstallWatcher.msi,默认安装在C:\Program Files (x86)\RedisWatcher,修改watcher.conf # re ...
- 【转载】微服务架构的基础框架选择:Spring Cloud还是Dubbo?
微服务框架选型,原文链接请参见:http://blog.didispace.com/microservice-framework/ http://blog.csdn.net/zeb_perfect/a ...
- C#中遍历DataTable类型并删除行数据
从数据库中读取出了DataSet类型的数据,通过dataSet.Tables[0]获得DataTable类型的数据. 这时候如果想批量修改dataTable中的内容,比如要删除dataTable中co ...