zoukankan      html  css  js  c++  java
  • Leetcode: ZigZag Conversion

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

    一道数学题,参考了网上的思路,先计算一下每一个zig包含的字符个数,实际上是zigSize = 2*nRows – 2
    然后一行一行的加s中的特定元素就行。
    第一行和最后一行都只需要加一个字符,每一个zig,而且在s中的index间隔是zigSize。
    中间每一行需要添加两个字符到结果中去。第一个字符同上;第二个字符的index就是zigSize – i。i是行index。

     需要注意的是,nRows == 1一定要单独讨论,否则rsize = 2*nRows - 2 == 0,循环没有增量会成为死循环。还有18-23行添加的顺序谁先谁后是有讲究的。

    第三遍代码:361ms

     1 public class Solution {
     2     public String convert(String s, int nRows) {
     3         if (s == null || s.length() == 0) return "";
     4         if (s.length() <= nRows || nRows == 1) return s;
     5         int rsize = 2 * nRows - 2;
     6         StringBuffer res = new StringBuffer();
     7         for (int i=0; i<nRows; i++) {
     8             if (i == 0 || i == nRows-1) {
     9                 int t = 0;
    10                 while (i + t * rsize < s.length()) {
    11                     res.append(s.charAt(i+t*rsize));
    12                     t++;
    13                 }
    14             }
    15             else {
    16                 int m = 0;
    17                 while (i + m * rsize < s.length() || (rsize - i + m * rsize) < s.length()) {
    18                     if (i + m * rsize < s.length()) {
    19                         res.append(s.charAt(i+m*rsize));
    20                     }
    21                     if ((rsize - i + m * rsize) < s.length()) {
    22                         res.append(s.charAt(rsize-i+m*rsize));
    23                     }
    24                     m++;
    25                 }
    26             }
    27         }
    28         return res.toString();
    29     }
    30 }

     如果不分开i =0, i=nRows-1和其他case, 444ms, 比上面反而慢一点

     1 public class Solution {
     2     public String convert(String s, int nRows) {
     3         if (s == null || s.length() == 0) return "";
     4         if (s.length() <= nRows || nRows == 1) return s;
     5         int rsize = 2 * nRows - 2;
     6         StringBuffer res = new StringBuffer();
     7         for (int i=0; i<nRows; i++) {
     8                 int m = 0;
     9                 while (i + m * rsize < s.length() || (rsize - i + m * rsize) < s.length()) {
    10                     if (i + m * rsize < s.length()) {
    11                         res.append(s.charAt(i+m*rsize));
    12                     }
    13                     if (i!=0 && i!=nRows-1 && ((rsize - i + m * rsize) < s.length())) {
    14                         res.append(s.charAt(rsize-i+m*rsize));
    15                     }
    16                     m++;
    17                 }
    18 
    19         }
    20         return res.toString();
    21     }
    22 }
  • 相关阅读:
    二十一.组合模式
    二十四.桥接模式
    二十六.职责链模式
    二十五.命令模式
    将小写转化成大写
    备份JOB SCHEDULE ENTRY的简单方法
    如何确定哪一个作业锁定QDLS下的一个目标
    WRKACTJOB命令一些有用功能介绍
    如何使用CA/400批处理的方式传输数据
    用前缀给字段命名
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/3781136.html
Copyright © 2011-2022 走看看