当前位置:网站首页>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/
边栏推荐
- [reach out to Party welfare] developer reload system sequence
- Error: plug ins declaring extensions or extension points must set the singleton directive to true
- FCN全卷積網絡理解及代碼實現(來自pytorch官方實現)
- Pyramid Scene Parsing Network【PSPNet】论文阅读
- Cygwin的下载和安装配置
- Listener listener
- 还在浪费脑细胞自学吗,这份面试笔记绝对是C站天花板
- Leetcode 31 next spread, leetcode 64 minimum path sum, leetcode 62 different paths, leetcode 78 subset, leetcode 33 search rotation sort array (modify dichotomy)
- 二叉树神级遍历:Morris遍历
- Include() of array
猜你喜欢

ASGNet论文和代码解读2

线程数据共享和安全 -ThreadLocal

Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C

4、【WebGIS实战】软件操作篇——数据导入及处理

Leetcode 31 next spread, leetcode 64 minimum path sum, leetcode 62 different paths, leetcode 78 subset, leetcode 33 search rotation sort array (modify dichotomy)

Appium automation test foundation -- supplement: c/s architecture and b/s architecture description

MFC窗口滚动条用法

pytorch训练深度学习网络设置cuda指定的GPU可见

Explain spark operation mode in detail (local+standalone+yarn)

数据库中COMMENT关键字的使用
随机推荐
数据库DDL(Data Definition Language,数据定义语言)知识点
Appium automation test foundation -- supplement: c/s architecture and b/s architecture description
Addition without addition, subtraction, multiplication and division
使用selenium自动化测试工具爬取高考相关院校专业招生分数线及排名情况
LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
409. 最长回文串
JS daily development tips (continuous update)
C语言的sem_t变量类型
【JPCS出版】2022年第三届控制理论与应用国际会议(ICoCTA 2022)
【TA-霜狼_may-《百人计划》】2.3 常用函数介绍
Filter
241. 为运算表达式设计优先级
Pathmeasure implements loading animation
Blueprism registration, download and install -rpa Chapter 1
Test function in pychram
Thread data sharing and security -threadlocal
Ouc2021 autumn - Software Engineering - end of term (recall version)
187. 重复的DNA序列
Feign remote call and getaway gateway
衡量两个向量相似度的方法:余弦相似度、pytorch 求余弦相似度:torch.nn.CosineSimilarity(dim=1, eps=1e-08)