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

字符串的弯曲转换。

要做的首先就是发现每一行的规律,能够发现,每一行有2 * nRows - 2个字符。把结果的列先增加到新的字符串中。再增加行。

    public String convert(String s, int nRows) {
if(nRows <= 1)
return s;
StringBuffer buf = new StringBuffer();
int period = 2 * nRows - 2;
for(int i=0;i<nRows;i++){
for(int j=i;j<s.length();j+=period){
buf.append(s.charAt(j));
if(i !=0 && i != nRows - 1 && j+period - 2*i <s.length())
buf.append(s.charAt(j+period - 2*i));
}
}
return buf.toString();
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

LeetCode——ZigZag Conversion的更多相关文章

  1. 6.[leetcode] ZigZag Conversion

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

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

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

  3. LeetCode: ZigZag Conversion 解题报告

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

  4. [leetcode]ZigZag Conversion @ Python

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

  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. hdu1881(贪心+dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1881 分析:按照结束时间从小到大排序,然后以每个结束点为容量进行01背包,选入的必定符合条件的. 因为 ...

  2. URAL1113(数学)

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1113 根据样例分析: 1.沙漠只有500公里或者更短,这时很简单,一次搞定. 2.沙漠6 ...

  3. ZOJ 1584:Sunny Cup 2003 - Preliminary Round(最小生成树&amp;&amp;prim)

    Sunny Cup 2003 - Preliminary Round April 20th, 12:00 - 17:00 Problem E: QS Network In the planet w-5 ...

  4. Linux Shell脚本入门--grep命令详解

    grep简介<摘自鸟哥,并加以整理.> grep (global search regular expression(RE) and print out the line,全面搜索正则表达 ...

  5. 【十一年】注入框架RoboGuice采用:(Your First Injection into a Custom View class)

    上一篇我们简单的介绍了一下RoboGuice的使用([十]注入框架RoboGuice使用:(Your First Testcase)),今天我们来看下自己定义View的注入(Custom View). ...

  6. Eclipse关闭检查

    有没有发现每次操作Eclipse过后,都会有build Workspace的操作一直在后台执行,JS校验一直validate,非常卡非常受不了有木有? 有木有?以下是我个人成功的步骤,曾经试过非常多次 ...

  7. 在Amazon AWS RHEL 7上安装 配置PPTP VPN

    0 前言 0.1 为什么需要VPN? 国内的VPN不是必须,但是国外的VPN是很有用的.连接到国外的VPN服务器之后就可以访问Google,Facebook, Youtube等网站,没有Google的 ...

  8. UVALive 3890 Most Distant Point from the Sea(凸包最大内接园)

    一个n个点的凸多边形,求多边形中离多边形边界最远的距离.实际上就是求凸包最大内接圆的半径. 利用半平面交求解,每次二分枚举半径d,然后将凸包每条边所代表的半平面沿其垂直单位法向量平移d,看所有平移后的 ...

  9. [Web Chart系列之五] 6. 实战draw2d之ConnectionRouter

    前言 ConnectionRouter 的作用是定义连线的展示样式. 是直线连接还是曲线连接(好像也是基于Bezier曲线) 位于包: draw2d.layout.connection 下. 常见的有 ...

  10. 使用NaturalDuration获取音频的时长

    #region customizeTime ) sec = " + mediaElement.Position.Seconds.ToString(); else sec = mediaEle ...