当前位置:网站首页>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/
边栏推荐
- Take you through a circuit board, from design to production (dry goods)
- Feature Pyramid Networks for Object Detection论文理解
- Appium fundamentals of automated testing - basic principles of appium
- Leetcode:829. Sum of continuous integers
- Filter
- Blueprism registration, download and install -rpa Chapter 1
- How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars
- You cannot right-click F12 to view the source code solution on the web page
- 数据库中COMMENT关键字的使用
- Error: plug ins declaring extensions or extension points must set the singleton directive to true
猜你喜欢
【TA-霜狼_may-《百人计划》】2.1 色彩空间
使用selenium自动化测试工具爬取高考相关院校专业招生分数线及排名情况
FCN全卷积网络理解及代码实现(来自pytorch官方实现)
Download and installation configuration of cygwin
Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
Nacos
Pyramid Scene Parsing Network【PSPNet】论文阅读
深度学习中的随机种子torch.manual_seed(number)、torch.cuda.manual_seed(number)
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
5. [WebGIS practice] software operation - service release and permission management
随机推荐
Take you through a circuit board, from design to production (dry goods)
过滤器 Filter
FCN全卷积网络理解及代码实现(来自pytorch官方实现)
pytorch nn. AdaptiveAvgPool2d(1)
10. 正则表达式匹配
pytorch中的双线性插值上采样(Bilinear Upsampling)、F.upsample_bilinear
torch. histc
The preorder traversal of leetcode 144 binary tree and the expansion of leetcode 114 binary tree into a linked list
Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C
在 C 中声明函数之前调用函数会发生什么?
Leetcode 128 longest continuous sequence (hash set)
Complete knapsack problem
Cookie&Session
网页不能右键 F12 查看源代码解决方案
4. [WebGIS practice] software operation chapter - data import and processing
The difference between MFC for static libraries and MFC for shared libraries
Server rendering technology JSP
排序链表(归并排序)
用小程序的技术优势发展产业互联网
[小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation