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".

原题链接:https://leetcode.com/problems/zigzag-conversion/

Analysis & Solution

There exits pattern in the ZigZag Conversion. The indexs at i row follows patterns:

Let DiffConstant = 2*row - 2

i = 0: the difference = DiffConstant

i = 1: the differences = 2(row-1) - 2, DiffConstant - (2(row-1) - 2).

i = 2: the differences = 2(row-2) - 2, DiffConstant - (2(row-2) - 2).

i = k: the differences = 2(row-k) - 2, DiffConstant - (2(row-k) - 2) = 2k

 public String convert(String s, int numRows) {
if(numRows == 1){
return s;
}
int diffConstant = 2*numRows - 2, i, index;
StringBuffer res = new StringBuffer();
for(i = 0; i < numRows; i++){
index = i;
boolean isEven = true;
while(index < s.length()){
res.append(s.charAt(index));
if(i == 0 || i == numRows -1){
index += diffConstant;
}else{
if(isEven){
index += 2*(numRows-i) - 2;
}else{
index += 2*i;
}
isEven = !isEven;
}
}
}
return res.toString();
}

6.[leetcode] ZigZag Conversion的更多相关文章

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

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

  2. LeetCode: ZigZag Conversion 解题报告

    ZigZag ConversionThe string "PAYPALISHIRING" is written in a zigzag pattern on a given num ...

  3. [leetcode]ZigZag Conversion @ Python

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

  4. [LeetCode]ZigZag Conversion

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

  5. LeetCode——ZigZag Conversion

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

  6. [LeetCode] ZigZag Conversion [9]

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

  7. C++ leetcode::ZigZag Conversion

    mmp,写完没保存,又得重新写.晚上写了简历,感觉身体被掏空,大学两年半所经历的事,一张A4纸都写不满,真是一事无成呢.这操蛋的生活到底想对我这个小猫咪做什么. 今后要做一个早起的好宝宝~晚起就诅咒自 ...

  8. Leetcode:ZigZag Conversion分析和实现

    问题的大意就是将字符串中的字符按锯齿状(倒N形)垂直由上向下放置,最后水平从左向右读取.比如 ABCDEFGHIJKLMN,4表示 A          G      M B      F  H    ...

  9. LeetCode解题报告—— 2 Keys Keyboard & Longest Palindromic Substring & ZigZag Conversion

    1. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...

随机推荐

  1. 修复lvm的逻辑卷

    一.背景 公司传统的服务器不知道什么朝代的朝臣用lvm分区,1T的硬盘分了50G挂载到根目录"/"里面有/var./usr--,剩下的挂载到了"/home"目录 ...

  2. Windows Server 2008取消登录前的Ctrl+Alt+Delete组合键操作

    前言: 在Windows Server 2008服务器中,为了防止人们登录服务器时错误的将账户和密码输入其他地方导致信息泄漏,所以在我们登录Windows Server 2008服务器操作系统时会要求 ...

  3. plugin.go 源码阅读

    , nil)             }             if c.client != nil {                 c.client.Close()             } ...

  4. maintenance.go

    package, && req.Alarm == pb.AlarmType_NONE {         ar, err := m.AlarmList(ctx)         if ...

  5. 【NOIP模拟赛】随

    题目链接: 172.18.111.252:800/problem.php?cid=1001&pid=0 题解: 膜达神……(NOIP考这个就等爆零吧……) 显然我们得到一个结论:$ans=\s ...

  6. BZOJ_4002_[JLOI2015]有意义的字符串_矩阵乘法

    BZOJ_4002_[JLOI2015]有意义的字符串_矩阵乘法 Description B 君有两个好朋友,他们叫宁宁和冉冉.有一天,冉冉遇到了一个有趣的题目:输入 b;d;n,求 Input 一行 ...

  7. Applet 应用程序进行数字签名,对系统文件进行读写操作

    转:http://www.iteye.com/topic/154531 最近在研究applet,打算使用applet来开发一个上传文件上传控件,之前因为一直觉得applet的沙箱控制导致applet不 ...

  8. 计算机17-1,2作业D

    D.环形矩阵 Description 给定一个整数m,按m形成一个环形矩阵.如m=5,则环形矩阵为: 1   1   1   1   1   1   1   1   1    1   2   2   ...

  9. Python安装和配置

    在我厂呆了快一年,终于等来了转岗机会,而且现在正在调动到新成立的AI战略部门,心里无比欣喜和激动.自己作为一个小白,终于有机会踏入AI领域,离自己的梦想更近了一步,个人感到无比的幸运,仿佛天生就有上天 ...

  10. keras实现简单CNN人脸关键点检测

    用keras实现人脸关键点检测 改良版:http://www.cnblogs.com/ansang/p/8583122.html 第一步:准备好需要的库 tensorflow  1.4.0 h5py ...