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. Autofac.Integration.Mvc.Owin分析

    using System; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Secur ...

  2. sqlserver下载

    https://msdn.microsoft.com/zh-cn/sqlserver/default.aspx

  3. Web Pages - Efficient Paging Without The WebGrid

    Web Pages - Efficient Paging Without The WebGrid If you want to display your data over a number of p ...

  4. JS验证URL正则

    方法一: function fIsUrL(sUrl) { var sRegex = '^((https|http|ftp|rtsp|mms)?://)' + '?(([0-9a-z_!~*\'().& ...

  5. 在ASP.MVC中使用Ajax

    Asp.net MVC 抛弃了Asp.net WebForm那种高度封装的控件,让我们跟底层的HTML有了更多的亲近.可以更自由.更灵活的去控制HTML的结构.样式和行为.Asp.net MVC可以更 ...

  6. 缓存算法(页面置换算法)-FIFO、LFU、LRU

    在前一篇文章中通过leetcode的一道题目了解了LRU算法的具体设计思路,下面继续来探讨一下另外两种常见的Cache算法:FIFO.LFU 1.FIFO算法 FIFO(First in First ...

  7. MobClick详解

    1.使用自定义事件 使用自定义事件功能请先在网站应用管理后台(设置->编辑自定义事件)中添加相应的自定义事件后,服务器才会对相应的自定义事件请求进行处理.这里我们将提供几个简单而通用的接口: 1 ...

  8. fedora23忘记root密码怎么办??

    fedora23使用的是uefi, 不是 传统的grub 所以在编辑grub的时候, 跟以前的版本略有不同 最最重要的是: 在编辑启动条目的时候, 那个 linuxefi ... vmlinuz... ...

  9. python中raw_input() 与 input()

    参考网址:http://www.cnblogs.com/way_testlife/archive/2011/03/29/1999283.html 在python中如何接收一个输入的字符串. 举个例子: ...

  10. php开发总结

    ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql=shared,mys ...