problem0006

package
v0.0.0-...-899dd15 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2020 License: MIT Imports: 1 Imported by: 0

README

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: "PAHNAPLSIIGYIR" Write the code that will take a string and make this conversion given a number of rows:

func convert(text string, nRows int) string

convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".

解题思路

输入"ABCDEFGHIJKLMNOPQRSTUVWXYZ"和参数5后,得到答案"AGMSYBFHLNRTXZCEIKOQUWDJPV", 按照题目的摆放方法,可得:

A   I   Q   Y
B  HJ  PR  XZ
C G K O S W
DF  LN  TV
E   M   U

可以看到,各行字符在原字符串中的索引号为

  1. 0行,0, 8, 16, 24
  2. 1行,1, 7,9, 15,17, 23,25
  3. 2行,2, 6, 10, 14, 18, 22
  4. 3行,3,5, 11,13, 19,21
  5. 4行,4, 12, 20

令p=numRows×2-2,可以总结出以下规律

  1. 0行, 0×p,1×p,...
  2. r行, r,1×p-r,1×p+r,2×p-r,2×p+r,...
  3. 最后一行, numRow-1, numRow-1+1×p,numRow-1+2×p,...

只需编程依次处理各行即可。

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL