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 ...
随机推荐
- 〖Linux〗Ubuntu13.10,配置tftp服务器
前言,配置了好久没有发现老是出问题tftp: server error: (2) Access violation,一般侦测之后... 1. 安装软件包:apt-getsudo apt-get ins ...
- linux shutdown命令
shutdown [-t 秒] [-arkhncfF] 时间 [警告信息] 常用选项与参数: -t sec : -t 后面加秒数,亦即『过几秒后关机』的意思 -k : 不要真的关机,只是发送警告信息出 ...
- DevExpress GridControl 动态创建字段及主细关系表过程
原文地址:http://hi.baidu.com/qdseashore/item/38f1153e9d0143637d034b7a 在做项目中,往往需要在查询基类模板窗口内做主细关系Grid,引用一下 ...
- 交叉编译OpenWrt 定制固件
在Centos7上交叉编译生成OpenWrt固件 安装ss-* 获取最新的ss, 当前是 wget https://github.com/shadowsocks/shadowsocks-libev/a ...
- (原)Ubuntu16中安装nvidia的显卡驱动
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5638185.html part1 直接在“软件和更新-附加驱动”里面设置 安装完ubuntu16后,显 ...
- HDUOJ---Can you solve this equation?
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- iOS - CFNetwork 的使用
1.CFNetwork CFNetwork 是基于 OS 层 BSDSocket 封装(纯 C),用于网络通信,早期的网络请求框架 ASIHTTPRequest 就是基于 CFNetwork 进行的封 ...
- 【js】js中的||和&&
逻辑与&&和逻辑或||操作符可以应用于任何类型的操作数,而不仅仅是布尔值. 几乎所有语言中||和&&都遵循“短路”原理, 如&&中第一个表达式为假就不会 ...
- 什么是IIS应用程序池
IIS应用程序池是将一个或多个应用程序链接到一个或多个工作进程集合的配置.因为应用程序池中的应用程序与其他应用程序被工作进程边界分隔,所以某个应用程序池中的应用程序不会受到其他应用程序池中应用程序所产 ...
- Python学习笔记011——内置函数eval()
1 描述 eval() 函数用来执行一个字符串表达式,并返回表达式的值 2 语法 原文 eval(expression[, globals=None[, locals=None]]) express ...