zoukankan      html  css  js  c++  java
  • leetcode-6-ZigZag Conversion

    题目: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 s, int numRows);

    Example 1:

    Input: s = "PAYPALISHIRING", numRows = 3
    Output: "PAHNAPLSIIGYIR"
    

    Example 2:

    Input: s = "PAYPALISHIRING", numRows = 4
    Output: "PINALSIGYAHRPI"
    Explanation:
    
    P     I    N
    A   L S  I G
    Y A   H R
    P     I

    思路:我觉得这就是一个找规律的题,写出一个字符串后,从第一行开始找,寻找每一行字母之间的关系
    举例:
    对于字符串s,设总行数为n=5,
    s="PAYPALISHIRING"
    P       H               one
    A     S I               two
    Y   I   R               three
    P L     I  G            four
    A N five

    行数 起始字母下标 本行下一个字母的位置偏移数
    one 0 下:(5-one)*2               
                   上:0
    two       1              下:(5-two)*2
                   上:(two-1)*2
    three 2 下:(5-three)*2
                   上:(three-1)*2
    four 3 下:(5-four)*2
    上:(four-1)*2
    five 4 下:(5-five)*2=0
                 上:(5-1)*2=8
    其中,下 表示的是从该字母往下走,折一下后到达本行的下一个字母需要的经历的字母数量
    同理,上 表示的是从该字母往上走,折一下后到达本行的下一个字母需要的经历的字母数量
    比如对于第一行的字母p,往下经过AYPALIS后到达本行的第二个字母H,对于A-H这条路径,除了上下边界的两行,中间每行夹杂的字母数为2,如果算上H本身的位置,
    那么H的位置坐标便是0+(总行数-P字母下面所剩的行数)*2

    同理,若果要算上的话,就是 当前位置+(当前行-第一行)*2;(当前行-第一行)表示的是当前位置往上走折一下后到达本行的下一个字母所经过的行数
     

    代码如下:

     1 class Solution {
     2     public  String convert(String s, int n) {
     3         if(s.length()<=n||n == 1) return s;
     4                 StringBuilder sb=new StringBuilder();
     5                 for(int row=1;row<=n;row++){
     6                     int firstChar=row-1;
     7                     int down=(n-row)*2;
     8                     int up=(row-1)*2;
     9                     sb.append(s.charAt(firstChar));
    10                     while(firstChar<s.length()){
    11                         if(down+firstChar>=s.length())break;
    12                         else if(down!=0){
    13                             sb.append(s.charAt(firstChar+down));
    14                             firstChar=firstChar+down;
    15                         }
    16                         if(up+firstChar>=s.length()) break;
    17                         else if(up!=0) {
    18                             sb.append(s.charAt(firstChar+up));
    19                             firstChar=firstChar+up;
    20                         }
    21                     }
    22                 }
    23                 return sb.toString();
    24     }
    25 }

    You are here! 
    Your runtime beats 99.22 % of java submissions.哈哈,感觉该没这么快吧。。。



  • 相关阅读:
    ios 中的循环引用问题及解决
    Leetcode-Maximum Depth of Binary Tree
    Leetcode-Min Stack
    Leetcode-Intersection of Two Linked Lists
    Leetcode-Binary Tree Inorder Traversal
    Leetcode-Unique Binary Search Trees II
    Leetcode-Unique Binary Search Trees
    Leetcode-binary Tree Zigzag Level Order Traversal
    Leetcode-Construct Binary Tree from inorder and preorder travesal
    Leetcode-Next Permutation
  • 原文地址:https://www.cnblogs.com/pathjh/p/9129572.html
Copyright © 2011-2022 走看看