问题的大意就是将字符串中的字符按锯齿状(倒N形)垂直由上向下放置,最后水平从左向右读取。比如

ABCDEFGHIJKLMN,4表示

A          G      M

B      F  H    L  N

C  E      I  K

D          J

按水平顺序读取的结果则为AGMBFHLNCEIKDJ。(原题目的描述就很有问题)


解决方案非常简单,可以发现字符串中第i个字符,其水平右边最近的元素为i+2*(row-1),其中row为规定的行高。而除了第一行和最后一行,中间的行的处理逻辑是一致的,而首行和尾行的处理逻辑也是一致的。

res = ""

step = 2 * (row - 1)

//处理首行

for(int i = 0; i < s.length; i = i + step)

  res = res + s[i]

//处理第1~row-2行

for(int i = 1; i< row - 1; i = i + 1)

  //添加处于垂直列中的元素,如果有必要还要添加中间斜线上的元素

  for(int j = i; j < s.length; j = j + step)

    res = res + s[j]

    if(j + step - i * 2 < s.length)

      res = res + s[j + step - i * 2]

//处理尾行

for(int i = row - 1; i < s.length; i = i + step)

  res = res + s[i]

由于每次循环体内的有效代码的执行都会将结果的长度增加1,但是结果的长度必定与s的长度一致,因此循环体最多只会执行s.length次,记n=s.length,因此这段代码的时间复杂度和空间复杂度均为O(n)。


最后惯例附上代码:

 package cn.dalt.leetcode;

 /**
  * Created by dalt on 2017/6/9.
  */
 public class ZigZagConversion {
     public String convert(String s, int numRows) {
         int slen = s.length();
         if (numRows == 1) {
             return s;
         }
         StringBuilder sb = new StringBuilder(slen);
         int totalstep = numRows + numRows - 2;
         //The first line
         for (int i = 0, bound = slen; i < bound; i += totalstep) {
             sb.append(s.charAt(i));
         }
         //The following line
         for (int i = 1, iBound = numRows - 1; i < iBound; i++) {
             int step = totalstep - i * 2;
             for (int j = i, jBound = slen; j < jBound; j += totalstep) {
                 sb.append(s.charAt(j));
                 if (j + step < jBound) {
                     sb.append(s.charAt(j + step));
                 }
             }
         }

         //The last line
         for (int i = numRows - 1, iBound = slen; i < iBound; i += totalstep) {
             sb.append(s.charAt(i));
         }
         return sb.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

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

  7. [LeetCode] ZigZag Conversion [9]

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

  8. C++ leetcode::ZigZag Conversion

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

  9. leetcode题解 6.ZigZag Conversion

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

随机推荐

  1. string manipulation in game development-C # in Unity -

    ◇ string manipulation in game development-C # in Unity - It is about the various string ● defined as ...

  2. 有状态与无状态 cookie session

    服务器所维护的与客户交互活动的信息称为状态信息.不保存任何状态信息的服务器称为无状态服务器(stateless server),反之则称为有状态服务器(stateful server). 面向连接对应 ...

  3. iTunes文件共享

    在Info.plist文件中添加UIFileSharingEnabled键,并将键值设置为YES.将您希望共享的文件放在应用程序的Documents目录.

  4. 关于modelsim添加库的说明

    声明:以下纯属个人习惯. 1.工程建好后添加一个编译好的库的方法是:file->new->library选择a map to an existing library.然后将这个库在你这工程 ...

  5. vba打开excel文件遍历sheet的名字和指定单元格的值

    今天项目上有个应用,获取指定Excel文件下的所有sheet的名称以及当前sheet中指定单元格的值,并把他们写到固定的sheet中去,看了下,文件比较多,而且每个文件sheet的个数比较多,也不一样 ...

  6. Ubuntu apt-get卸载小记

    过sudo apt-get install xxxx 安装软件后,总是无法卸载干净,这里以Apache 为例,提供方法:首先sudo apt-get remove apache2再sudo apt-g ...

  7. 整数a整数b

    namespace ConsoleApplication6{ class Program { static void Main(string[] args) { while (true) { Cons ...

  8. 1088 Rational Arithmetic

    题意: 给出两个分式(a1/b1 a2/b2),分子.分母的范围为int型,且确保分母不为0.计算两个分数的加减乘除,结果化为最简的形式,即"k a/b",其中若除数为0的话,输出 ...

  9. PHP5.3.8连接Sql Server SQLSRV30

    PHP5.3连接SQL Server就不能用php_mssql.dll了. 网上下载了好多都不行,因为它的版本是5.2的,不能再PHP5.3中使用. 后来听说微软专门为PHP出了自己的dll. 叫做M ...

  10. 《转载》ubuntu Sublime text 3 解决中文输入问题

    其实,在这个文章之前,网上都有好多教程了.不知道是不是因为复制黏贴的传播太多,导致有些字符串的丢失,导致编译失败,so库文件无法载入,从而不能输入中文.折腾了许久之后,终于搞定了.记录下来,方便自己下 ...