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. iOS推送:Java服务器端发送表情(绘文字)

    http://blog.csdn.net/musou_ldns/article/details/8692520 功能的时候,客户要求能够给iphone发送表情图标,也就是绘文字. 手机环境:iOS5. ...

  2. 巩固java(二)----JVM堆内存结构及垃圾回收机制

    前言:        我们在运行程序时,有时会碰到内存溢出(OutOfMemoryError)的问题,为了解决这种问题,我们有必要了解JVM的内存结构和垃圾回收机制. 正文: 1.JVM堆内存结构   ...

  3. Android 自定义ViewGroup手把手教你实现ArcMenu

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37567907 逛eoe发现这样的UI效果,感觉很不错,后来知道github上有这 ...

  4. selenium相关技术研究(从1.0-3.0)

    注: 以下内容引自http://www.cnblogs.com/hhudaqiang/p/6550135.html Selenium相关技术研究(从1.0-3.0) 好吧,最近看wxpython有点多 ...

  5. RabbitMQ 集群与网络分区(理论知识)

    关于network partition网络设备故障导致的网络分裂.比如,存在A\B\C\D\E五个节点,A\B处于同一子网,B\C\D处于另外一子网,中间通过交换机相连.若两个子网间的交换机故障了即发 ...

  6. admin-handlers.go

    package],,) ],,) ],,) ],,) ],,) ])     if err == redis.Nil {         http.NotFound(w, r)     } else ...

  7. 我的新纪元Day01

    在我刚刚开通博客园后,想了好久.不知道第一次随笔该写点什么,想写些自己学到的知识,但技术上还是菜鸟的我完全不知道我能向别人分享什么,想到这里有些沮丧. 但万事开头难,只要我入了门,广阔的编程语言的世界 ...

  8. Charles 连接手机抓包出现Unknown,一直无法抓包的问题解决

    mac电脑安装了charles并且确保已经安装成功,https抓包需要安装的相关的证书已经安装,并且手机浏览器中输入chls.pro/ssl已经将证书下载完成,但是手机与电脑相连通过点击手机应用还是无 ...

  9. Android--性能测试关注的指标

    性能测试过程中,出现的一些问题可直接导致了用户对当前app的使用率和卸载率,如果app使用时卡顿严重或者加载页面慢,cpu占用率高,导致app闪退等问题,在测试过程中,则需特别关注性能方面的体验,ap ...

  10. 安卓开发笔记(三十一):shape标签下子类根结点的具体使用

    在我的上一篇博文当中阐述了我们如何使用shape标签进行自定义控件,这里对shape控件的属性进行阐述,不知道如何使用这些属性的可以参见我的上一篇博文(自定义Button):https://www.c ...