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 解题报告的更多相关文章

  1. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  2. 6.[leetcode] ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  3. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  4. LeetCode - Course Schedule 解题报告

    以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...

  5. LeetCode ZigZag Conversion(将字符串排成z字型)

    class Solution { public: string convert(string s, int nRows) { string a=""; int len=s.leng ...

  6. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  7. 【LeetCode】281. Zigzag Iterator 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 deque 日期 题目地址:https://leetc ...

  8. [leetcode]ZigZag Conversion @ Python

    原题地址:https://oj.leetcode.com/problems/zigzag-conversion/ 题意: The string "PAYPALISHIRING" i ...

  9. [LeetCode] ZigZag Conversion [9]

    称号 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...

随机推荐

  1. Hibernate3.x异常No row with the given identifier exists 解决方法

    这个异常是在 多对一关系映射时,一方表中对应的数据不存在才抛出的.原来的配置: <many-to-one class="com.art.model.user.UserInfo" ...

  2. 浅析android应用增量升级(转)

    By 何明桂(http://blog.csdn.net/hmg25) 转载请注明出处 很久没有更新博客了,真是堕落啊,几次想提起笔,却总是被各种琐事耽搁,以后会多写文章记录点滴. 背景         ...

  3. 【FinacialKnowledge】财务报表及名词解释

    1.财务报表 以下三张表为:资产负债表.利润表.现金流量表                                                                        ...

  4. WeUI 是由微信官方设计团队专为微信移动 Web 应用设计的 UI 库

    WeUI 是由微信官方设计团队专为微信移动 Web 应用设计的 UI 库. WeUI 是一套同微信原生视觉体验一致的基础样式库,为微信 Web 开发量身设计,可以令用户的使用感知更加统一.包含butt ...

  5. 面试题 Comparable、Comparator 比较

    Comparable 用作默认的比较方式 Comparator 用作自定义的比较方式,当默认的比较方式不适用时或者没有提供默认的比较方式,使用Comparator就非常有用. 像Arrays和Coll ...

  6. iOS - Analyze 静态分析

    1.Analyze 使用 Xcode 自带的静态分析工具 Product -> Analyze(快捷键 command + shift + B)可以找出代码潜在错误,如内存泄露,未使用函数和变量 ...

  7. 【js】批量判断表单中的文本框非空

    方法一: <script type=”text/javascript”> /* * 批量验证表单非空 * 需要非空验证控件的样式class=”mustadd” */ $(".mu ...

  8. JMeter学习笔记--JMeter监听器

    监听器(Listeners)是一种展示采样结果的测试元件,采样结果可以通过树.表格.图片加以展示,或者简单地写入某个结果文件之中. 注:不同的监听器通过不同的方式展示服务器响应信息,但它们都将同样的原 ...

  9. Error_GL_KeyflexfieldDefinitionFactory.getStructureNumber无法找到应用产品

    2014-07-12 BaoXinjian

  10. OCR 介绍文章

    光学字符识别技术:让电脑像人一样阅读 微软亚洲研究院,霍强: https://www.msra.cn/zh-cn/news/features/ocr-20150720 文章,我们应该做什么样的研究 h ...