[LeetCode] 6. ZigZag Conversion (Medium)

原题链接

把字符串按照 ↓↓……的顺序,排列成一个 Z 形,返回 从左到右,按行读得的字符串。

思路:

建立一个二维数组来按行保存字符串。

按照 ↓↓……的方向进行对每一行加入字符。

太慢了这个解法,Runtime: 96 ms, faster than 3.61% of C++。

class Solution
{
public:
  string convert(string s, int numRows)
  {
    if (numRows <= 1)
      return s;
    string res;
    string zigZag[numRows];
    int len = s.length();
    int dirc = 1; // 1表示方向向下,-1表示方向向上
    int index = 0;
    for (int i = 0; i < len; i++)
    {
      zigZag[index] += s[i];
      if (index == numRows - 1)
        dirc = -1;

      if (index == 0)
        dirc = 1;
      index += dirc;
    }
    for (int i = 0; i < numRows; i++)
    {
      cout << zigZag[i] << endl;
      res += zigZag[i];
    }

    return res;
  }
};

原文地址:https://www.cnblogs.com/ruoh3kou/p/9906833.html

时间: 2024-10-17 16:39:00

[LeetCode] 6. ZigZag Conversion (Medium)的相关文章

leetcode -day16 ZigZag Conversion

1.  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 b

leetCode 6. ZigZag Conversion 字符串 (上传费劲)

6. ZigZag Conversion 题目:https://leetcode.com/problems/zigzag-conversion/ string convert2(string s, int numRows) { if (s.length() < 2 || numRows < 2) return s; int cycle = 2 * numRows - 2; string tmp; string result; for (int i = 0; i < numRows; i+

[LeetCode][JavaScript]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

【LeetCode】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

leetCode 6. ZigZag Conversion 字符串

6. ZigZag Conversion

LeetCode 6 - 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: "PAHN

LeetCode 6 ZigZag Conversion 模拟 难度:0

https://leetcode.com/problems/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

LeetCode 6 ZigZag Conversion(规律)

题目来源:https://leetcode.com/problems/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

[LeetCode][Python]ZigZag Conversion

# -*- coding: utf8 -*-'''__author__ = '[email protected]'https://oj.leetcode.com/problems/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