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://segmentfault.com/a/1190000005751185

打印题,二话不说,画图,标数字,找规律。
这题其实是EPI这本书里7.11题(Write a string sinusoidally),书里的题规定k=3,在leetcode里k作为参数,所以更难了。
leetcode的这个例子的图非常反人类,其实可以把它画得优美一些,像正弦函数的波浪形:

P       A       H       N
A P L S I I G
Y I R

举个别的例子自己画一下:

 public class Solution {
public String convert(String s, int nRows) { StringBuffer[] sb = new StringBuffer[nRows];
for (int i = ; i < sb.length; i++) {
sb[i] = new StringBuffer();
} int i = ;
while (i < s.length()) {
for (int idx = ; idx < nRows && i < s.length(); idx++) // vertically down
sb[idx].append(s.charAt(i++));
for (int idx = nRows-; idx >= && i < s.length(); idx--) // obliquely up
sb[idx].append(s.charAt(i++));
}
for (int idx = ; idx < sb.length; idx++)
sb[].append(sb[idx]);
return sb[].toString();
}
}

ZigZag Conversion的更多相关文章

  1. 【leetcode❤python】 6. ZigZag Conversion

    #-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object):    def convert(self, s, numRow ...

  2. 64. ZigZag Conversion

    ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...

  3. No.006 ZigZag Conversion

    6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...

  4. leetcode第六题 ZigZag Conversion (java)

    ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...

  5. leetcode题解 6.ZigZag Conversion

    6.ZigZag Conversion 题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a gi ...

  6. 6.[leetcode] ZigZag Conversion

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

  7. 字符串按照Z旋转90度然后上下翻转的字形按行输出字符串--ZigZag Conversion

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

  8. LeetCode--No.006 ZigZag Conversion

    6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...

  9. leetcode-algorithms-6 ZigZag Conversion

    leetcode-algorithms-6 ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag ...

  10. LeetCode 6. ZigZag Conversion & 字符串

    ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...

随机推荐

  1. Database Initialization Strategies in Code-First:

    You already created a database after running your Code-First application the first time, but what ab ...

  2. jsp日期插件My97DatePicker

    jsp日期插件My97DatePicker 强大的日期控件 使用方便简单 (2010-10-30 18:59:12) 转载▼ 标签: js javascript 日期插件 jsp it 分类: jqu ...

  3. centos 7.0 查看根目录下所有文件夹

    centos 7.0最小化安装 第一行是登录 [root@localhost ~]# [root@localhost ~]# cd ../ [root@localhost /]# ls bin dev ...

  4. Quartz.Net 基于XML配置启动

    1.App.config <configSections> <section name="quartz" type="System.Configurat ...

  5. CF451D Count Good Substrings (DP)

    Codeforces Round #258 (Div. 2) Count Good Substrings D. Count Good Substrings time limit per test 2 ...

  6. WAMP启动失败简单解决方法

    一般情况下,直接选择安装,突然出现问题了:提示:msvcp110.dll或msvcr110.dll问题, 那么你直接复制这个来百度就行. 在百度会提示让你一键安装并且修复的. 或者你可能会看网上其他教 ...

  7. JQuery中的html(),text(),val()区别

    jQuery中.html()用为读取和修改元素的HTML标签,.text()用来读取或修改元素的纯文本内容,.val()用来读取或修改表单元素的value值. 1.HTML html():取得第一个匹 ...

  8. AngularJS 使用ngOption实现下拉列表

    最近使用到了ngOption实现下拉选择列表,由于需要实现分组等功能,百度了下没有太好的文章,就百度到一篇英文的帖子,按照其中的代码很顺利的搞定了. 本篇根据文中代码,详细讲述下如何实现下拉列表 更多 ...

  9. WP8.1下 Cortana语音命令 VCD文件 设计

    Windows Phone8.1下的Cortana,可以通过语音的方式,打开.设置应用,进行页面跳转.执行任务. 我们先要创建VCD(VoiceCommand.xml)文件 <?xml vers ...

  10. 关于转录组比对STAR软件使用

    参考文章:http://weibo.com/p/23041883f77c940102vbkd?sudaref=passport.weibo.com 软件连接:https://github.com/al ...