当前位置:网站首页>6. zigzag transformation
6. zigzag transformation
2022-07-01 03:42:00 【Sun_ Sky_ Sea】
6. Z Font conversion
Original title link :https://leetcode.cn/problems/zigzag-conversion/
Will a given string s According to the given number of rows numRows , From top to bottom 、 Left to right Z Font arrangement .
For example, the input string is “PAYPALISHIRING” The number of rows is 3 when , Arranged as follows :
P A H N
A P L S I I G
Y I R
after , Your output needs to be read line by line from left to right , Generate a new string , such as :“PAHNAPLSIIGYIR”.
Please implement this function to transform a string into a specified number of lines :
string convert(string s, int numRows);
Example 1:
Input :s = “PAYPALISHIRING”, numRows = 3
Output :“PAHNAPLSIIGYIR”
Example 2:
Input :s = “PAYPALISHIRING”, numRows = 4
Output :“PINALSIGYAHRPI”
explain :
P I N
A L S I G
Y A H R
P I
Example 3:
Input :s = “A”, numRows = 1
Output :“A”
Tips :
1 <= s.length <= 1000
s By the English letters ( Lowercase and upper case )、‘,’ and ‘.’ form
1 <= numRows <= 1000
Their thinking :
Z Character assignment arrangement of shapes , Output is a printout by line , So there is numRows, The string on each line is set to s1,s2...sn, Each character c stay Z The row index corresponding to the shape is from s1 To sn, Then you encounter the first or last line , Change direction from sn To s1, This is repeated . So simulate such repeated operations , Calculate the string of each line , Finally, the answer to the question is to link the strings of all lines in line order .
Code implementation :
class Solution:
def convert(self, s: str, numRows: int) -> str:
# If there is only one line , Go straight back to s
if numRows < 2:
return s
# Initialize the string of each line to an empty string
# Finally, connect each line
res = ['' for _ in range(numRows)]
i = 0
# Reverse direction identification : Top to bottom or bottom to top , Here is the corresponding line
flag = -1
for c in s:
# The characters of the current line are organized
res[i] += c
# If you encounter the first or last line , according to Z shape
# If you need to change lines, change directions
if i == 0 or i == numRows - 1:
flag = -flag
i += flag
return ''.join(res)
reference :
https://leetcode.cn/problems/zigzag-conversion/solution/zzi-xing-bian-huan-by-jyd/
边栏推荐
- 排序链表(归并排序)
- 214. 最短回文串
- 【TA-霜狼_may-《百人计划》】2.4 传统经验光照模型
- How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars
- 二叉树神级遍历:Morris遍历
- Appium automation test foundation -- supplement: c/s architecture and b/s architecture description
- 4、【WebGIS实战】软件操作篇——数据导入及处理
- File upload and download
- RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
- idea插件备份表
猜你喜欢

Gorilla/mux framework (RK boot): RPC error code design

【TA-霜狼_may-《百人计划》】1.3纹理的秘密

静态库使用MFC和共享库使用MFC的区别

排序链表(归并排序)

Binary tree god level traversal: Morris traversal

IPv4和IPv6、局域网和广域网、网关、公网IP和私有IP、IP地址、子网掩码、网段、网络号、主机号、网络地址、主机地址以及ip段/数字-如192.168.0.1/24是什么意思?

二叉树神级遍历:Morris遍历

Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products

5、【WebGIS实战】软件操作篇——服务发布及权限管理

Test function in pychram
随机推荐
Take you through a circuit board, from design to production (dry goods)
Edge drawing: a combined real-time edge and segment detector
【JPCS出版】2022年第三届控制理论与应用国际会议(ICoCTA 2022)
[deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st
过滤器 Filter
IPv4和IPv6、局域网和广域网、网关、公网IP和私有IP、IP地址、子网掩码、网段、网络号、主机号、网络地址、主机地址以及ip段/数字-如192.168.0.1/24是什么意思?
[小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation
Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C
Appium自动化测试基础--补充:C/S架构和B/S架构说明
MFC窗口滚动条用法
Database DDL (data definition language) knowledge points
389. 找不同
How to use hybrid format to output ISO files? isohybrid:command not found
报错:Plug-ins declaring extensions or extension points must set the singleton directive to true
10、Scanner. Next() cannot read spaces /indexof -1
用小程序的技术优势发展产业互联网
171. Excel 表列序号
实现pow(x,n)函数
使用selenium自动化测试工具爬取高考相关院校专业招生分数线及排名情况
二叉树神级遍历:Morris遍历