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. 关闭xp防火墙

    在控制面版关闭防火墙 禁用“Security Center”服务 禁用“WindowsFirewall/InternetConnectionSharing(ICS)”服务 删除服务:开始运行CMD,命 ...

  2. 检查用户输入信息是否完整(vb.net实现)

        机房收费系统中.在将用户输入的信息封装到实体中作为參数传到B层之前,总要对用户输入的信息进行检查.我将这种检查分为两类: 合法性检查 完整性检查     所谓合法性检查,就是用户输入的信息是否 ...

  3. c-fmt-fn标签用法

      c-fmt-fn标签用法 CreateTime--2017年1月6日15:48:43 Author:Marydon 一.参考链接 http://blog.csdn.net/fmwind/artic ...

  4. html5 效果 按下鼠标数值自动增长

    <!doctype html> <html> <head> <style> * { margin:0; padding:0; } div { margi ...

  5. 金山PDF

    金山是个很不错的软件公司,金山出PDF,纯粹是完善生态圈!毕竟没FoxitReader专业对PDF的处理上! 官网:芝麻开门 下载:http://wdl1.cache.wps.cn/wps/downl ...

  6. linux利器expect的使用

    1.什么是expect在做系统管理时,我们很多时候需要输入密码,例如:连接 ssh,连接ftp,那么如何能做到不输入密码,我们需要有一个工具,能代替我们实现与终端的交互,它能够代替我们实现与终端的交互 ...

  7. 由select/epoll返回的非阻塞connect还会是EINPROGRESS状态吗?

    一般情况下,我们像下面代码中所示的这样使用非阻塞connect: #include <stdio.h> #include <stdlib.h> #include <str ...

  8. centos7安装MySQL5.7无法设置密码问题

    前言 在使用centos7系统yum方式安装MySQL5.7后 不知道默认密码是多少  知道后没办法修改? 一.找到MySQL密码 service mysqld start vim /var/log/ ...

  9. HDUOJ----(1031)Design T-Shirt

    Design T-Shirt Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  10. CoreData数据库升级

    如果IOS App 使用到CoreData,并且在上一个版本上有数据库更新(新增表.字段等操作),那在覆盖安装程序时就要进行CoreData数据库的迁移,具体操作如下: 1.选中你的mydata.xc ...