• 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 }
  • 相关阅读:
    PHP chop() 函数
    PHP bin2hex() 函数
    多个表关联或者有视图套视图,快速检查SQL语句中所有的表统计信息是否过期
    洛谷P3628 [APIO2010]特别行动队 斜率优化
    洛谷P3195 [HNOI2008]玩具装箱TOY 斜率优化
    bzoj4282 慎二的随机数列 树状数组求LIS + 构造
    校园网 入读统计 + 性质分析
    HAOI2006 受欢迎的牛 缩点
    普通平衡树 Treap
    洛谷P1563 玩具谜题 简单模拟
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/3781136.html
Copyright © 2020-2023  润新知