📜  在 N 行中连接 Zig-Zag 字符串

📅  最后修改于: 2022-05-13 01:56:10.008000             🧑  作者: Mango

在 N 行中连接 Zig-Zag 字符串

字符串“ PAYPALISHIRING ”在给定的行数上以锯齿形图案书写,如下所示:(您可能希望以固定字体显示此图案以提高可读性)

然后逐行阅读: PAHNAPLSIIGYIR

因此,对于给定的字符串str和一个整数N ,当str以行方式 Zig-Zag 方式写入时,任务是打印由连接N行形成的字符串。

例子:

方法:给定的问题是一个基于实现的问题,可以通过以下步骤解决

  • 创建一个包含N个字符串的数组arr[N]
  • 将方向初始化为“向下”,将行初始化为0 。方向指示当前指针是按行向上还是向下移动。
  • 遍历输入字符串,对每个字符执行以下操作。
    • 将当前字符附加到表示当前行的字符串中。
    • 如果行号为N – 1 ,则将方向更改为“向上”
    • 如果行号为0 ,则将方向更改为 'down'
    • 如果方向是“向下”,则执行 row++。否则行-。
  • 一个一个地打印arr[]的所有字符串。

下面是上述方法的实现:

C++
// C++ code for the above approach
#include 
using namespace std;
 
// Function that Prints concatenation of
// all rows of str's Zig-Zag fashion
void printZigZagConcat(string str, int n)
{
    if (n == 1)
    {
        cout << str << endl;
    }
    string res = "";
    string arr[n] = {""};
    bool down;
    int row = 0; // helps in building individual blocks of strings
 
    for (int i = 0; i < str.size(); i++)
    {
        arr[row].push_back(str[i]);
        if (row == n - 1)
        {
            down = false;
        }
        if (row == 0)
        {
            down = true;
        }
        if (!down)
            row--;
        else
            row++;
    }
 
    for (int i = 0; i < n; i++)
    {
        cout << arr[i];
    }
}
 
int main()
{
    // Driver Code
    string str = "PAYPALISHIRING";
    int N = 3;
    printZigZagConcat(str, N);
    return 0;
}
 
// This code is contributed by Potta Lokesh


Java
// Java code for the above approach
import java.util.*;
 
class GFG {
 
  // Function that Prints concatenation of
  // all rows of str's Zig-Zag fashion
  static void printZigZagConcat(String str, int n)
  {
    if (n == 1) {
      System.out.print(str + "\n");
    }
    String res = "";
    String[] arr = new String[n];
    for (int i = 0; i < n; i++)
      arr[i] = "";
    boolean down = false;
    int row = 0; // helps in building individual blocks
    // of Strings
 
    for (int i = 0; i < str.length(); i++) {
      if (row >= 0)
        arr[row] += (str.charAt(i));
      if (row == n - 1) {
        down = false;
      }
      if (row == 0) {
        down = true;
      }
      if (!down)
        row--;
      else
        row++;
    }
 
    for (int i = 0; i < n; i++) {
      System.out.print(arr[i]);
    }
  }
 
  public static void main(String[] args)
  {
    // Driver Code
    String str = "PAYPALISHIRING";
    int N = 3;
    printZigZagConcat(str, N);
  }
}
 
// This code is contributed by umadevi9616


Python3
# Python 3 program of the above approach
 
# Function that Prints concatenation of
# all rows of str's Zig-Zag fashion
def printZigZagConcat(str, n):
 
    # Corner Case (Only one row)
    if n == 1:
        print(str)
        return
 
    # Find length of string
    l = len(str)
 
    # Create an array of
    # strings for all n rows
    arr = ["" for x in range(l)]
 
    # Initialize index for
    # array of strings arr[]
    row = 0
 
    # Traverse through
    # given string
    for i in range(l):
 
        # append current character
        # to current row
        arr[row] += str[i]
 
        # If last row is reached,
        # change direction to 'up'
        if row == n - 1:
            down = False
 
        # If 1st row is reached,
        # change direction to 'down'
        elif row == 0:
            down = True
 
        # If direction is down,
        # increment, else decrement
        if down:
            row += 1
        else:
            row -= 1
 
    # Print concatenation
    # of all rows
    for i in range(n):
        print(arr[i], end="")
 
 
# Driver Code
str = "PAYPALISHIRING"
N = 3
printZigZagConcat(str, N)


Javascript


输出
PAHNAPLSIIGYIR

时间复杂度: O(N)
辅助空间: O(N)