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 ...
随机推荐
- 关于NHibernate中存在于Session中实例的3种状态的简单分析
在使用NHibernate的时候.在Session中会有3种状态. 1. 瞬时状态 (Transient) 由 new 命令开辟内存空间的对象,也就是平时所熟悉的普通对象. 如: Student st ...
- SDUT 1157-小鼠迷宫问题(BFS&DFS)
小鼠迷宫问题 nid=24#time" title="C.C++.go.haskell.lua.pascal Time Limit1500ms Memory Limit 65536 ...
- 公钥私钥与SSL的握手协议(转)
一,公钥私钥1,公钥和私钥成对出现2,公开的密钥叫公钥,只有自己知道的叫私钥3,用公钥加密的数据只有对应的私钥可以解密4,用私钥加密的数据只有对应的公钥可以解密5,如果可以用公钥解密,则必然是对应的私 ...
- MSSQL-SQL SERVER 分页原理
项目中用到的, 用心琢磨一下此SQL语句即可: SELECT TOP $row * FROM ( SELECT ROW_NUMBER() OVER (ORDER BY [ID] desc ...
- HDUOJ----2647Reward
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- CocoaPods的ruby问题 Error fetching http://ruby.taobao.org/:
今天安装了一个CocoaPods,在安装淘宝ruby是遇到了问题 bogon:~ zhch$ gem sources -a http://ruby.taobao.org/ Error fetching ...
- 【LeetCode】166. Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- 由ConcurrentLinkedQueue扯到线程安全 待整理
前几天项目总是报错,找了下原因. ConcurrentLinkedQueue 本身是一个基于链接节点的无界线程安全队列,你自己调用就不用考虑线程安全了吗? 结论是:原子性操作当然是线程安全的,非原子性 ...
- 一个进程发起多个连接和gethostbyname等函数
一.在前面讲过的最简单的回射客户/服务器程序中,一个客户端即一个进程,只会发起一个连接,只要稍微修改一下就可以让一个客户端发起多个连 接,然后只利用其中一个连接发送数据. 先来认识一个函数getsoc ...
- [转] 基本RS触发器
在触发器中,最简单的触发器是基本RS触发器,它由两个与-非门(或者两个或-非门)来组成. 图5.2.1(a)是由与-非门构成的基本RS触发器,由图看出,基本RS触发器有两个输入端(和)和两个输出端(和 ...