当前位置:网站首页>LeetCode 6. Zigzag transformation (n-shaped transformation)
LeetCode 6. Zigzag transformation (n-shaped transformation)
2022-07-02 16:40:00 【_ Liu Xiaoyu】
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”
// This question should be N Glyph conversion
class Solution {
public:
string convert(string s, int numRows) {
string re;
if(numRows == 1) return s;
// Looking for a regular
for(int j =0; j < numRows; j++)
{
if(j == 0 || j == numRows -1 )
{
for(int i = j; i<s.size(); i += 2 * numRows - 2)
re += s[i];
}
else
{
for(int i = j, k = 2 * numRows -2 - j; i < s.size() || k < s.size() ;i += 2 * numRows -2, k += 2 * numRows -2)
{
if(i < s.size()) re += s[i]; // Add the front one first
if(k < s.size()) re += s[k];
}
}
}
return re;
}
};
边栏推荐
- day4
- Ranger (I) preliminary perception
- 618深度复盘:海尔智家的制胜方法论
- How to use stustr function in Oracle view
- Yyds dry goods inventory # look up at the sky | talk about the way and principle of capturing packets on the mobile terminal and how to prevent mitm
- ROW_NUMBER()、RANK()、DENSE_RANK区别
- MySQL min() finds the minimum value under certain conditions, and there are multiple results
- 学生选课系统(山东农业大学课程设计)
- Leetcode --- longest public prefix
- How to choose the right kubernetes storage plug-in? (09)
猜你喜欢

PCL 点云镜像变换

Summary of multithreading and thread synchronization knowledge

Some problems about MySQL installation

Seal Library - installation and introduction

路由模式:hash和history模式

JS learning notes - first acquaintance

Unity uses ugui to set a simple multi-level horizontal drop-down menu (no code required)

Summary | three coordinate systems in machine vision and their relationships
![OSPF - route aggregation [(summary) including configuration commands] | address summary calculation method - detailed explanation](/img/8b/36be3191a7d71f4a8c8181eaed8417.jpg)
OSPF - route aggregation [(summary) including configuration commands] | address summary calculation method - detailed explanation

Yyds dry inventory uses thread safe two-way linked list to realize simple LRU cache simulation
随机推荐
结构体的内存对齐
忆当年高考|成为程序员的你,后悔了吗?
Mathematical analysis_ Notes_ Chapter 6: Riemann integral of univariate function
电脑管理员权限在哪里可以打开
去除router-link中的下划线
Yyds dry goods inventory hands-on teaching you to carry out the packaging and release of mofish Library (fishing Library)
Yyds dry inventory executor package (parameter processing function)
[fluent] dart data type number type (DART file creation | num type | int type | double type | num related API)
Foreign enterprise executives, continuous entrepreneurs, yoga and skiing masters, and a program life of continuous iteration and reconstruction
Today in history: Alipay launched barcode payment; The father of time-sharing system was born; The first TV advertisement in the world
LeetCode 1. 两数之和
OSPF - detailed explanation of NSSA area and full NSSA area (including configuration command), LSA type 7 lsa-7
The difference and usage of calloc, malloc and realloc functions
Ranger (I) preliminary perception
OSPF - route aggregation [(summary) including configuration commands] | address summary calculation method - detailed explanation
Global and Chinese market of switching valves 2022-2028: Research Report on technology, participants, trends, market size and share
What is Amazon keyword index? The consequences of not indexing are serious
Win11应用商店无法加载页面怎么办?Win11商店无法加载页面
AWS virtual machine expansion
How to use stustr function in Oracle view