当前位置:网站首页>Force buckle 6 Zigzag transformation
Force buckle 6 Zigzag transformation
2022-06-11 20:05:00 【Three watch ghost】
Title source :https://leetcode-cn.com/problems/zigzag-conversion/
General meaning :
Give a string and number of lines r, String from top to bottom 、 Left to right Z Font arrangement , Then, the arranged strings are sorted from left to right 、 Take them out from top to bottom 、
Ideas
Directly simulate the transformation process
According to the title , stay Z In the process of transformation , I will fill it down first r Characters , And then fill it on the right r - 2 Characters , This process can be regarded as a cycle t = r * 2 - 2, So there are n / t ( Rounding up , The last cycle may not be complete , But it doesn't affect ) A cycle
After calculating the period , It can be simulated according to the periodic change rules , To save memory , You can use it directly StringBuffer Represents the string corresponding to each line
simulation
- Find the period t
- Traversing the entire string , The index for i. Set the line mark at the beginning x = 0. So when i mod t < r - 1 when , You need to continue to fill in characters , Row number x + 1; Otherwise, fill in the characters to the right , Row number x - 1
- Index each time i Place the character at in the line marker x Corresponding StringBuffer End of object , Then based on the current index i Determine the number of rows that should be placed next time
Code :
public String convert(String s, int numRows) {
int n = s.length();
int r = numRows;
// If the number of rows is 1 Or greater than the string length , Directly return the original string
if (r == 1 || r >= n) {
return s;
}
// cycle
int t = r * 2 - 2;
// The string object corresponding to the row
StringBuffer[] sb = new StringBuffer[r];
for (int i = 0; i < r; i++) {
sb[i] = new StringBuffer();
}
// Traverse
for (int i = 0, x = 0; i < n; i++) {
// Put the current character on the corresponding line
sb[x].append(s.charAt(i));
// Determine the number of lines to put characters next time
if (i % t < r - 1) {
x++;
} else {
x--;
}
}
StringBuffer ans = new StringBuffer();
for (int i = 0; i < r; i++) {
ans.append(sb[i]);
}
return ans.toString();
}
边栏推荐
- 2022-2028 global and Chinese thermopile array module market status and future development trend
- STL application (unfinished
- [high precision] decimal integer addition
- In 2021, the global adult diaper revenue was about $11560million, which is expected to reach $15440million in 2028. From 2022 to 2028, the CAGR was 4.2%
- 27. this pointing problem
- Implementation of SQL online editor based on Vue + codemirror
- POJ 1458 longest common subsequence (dynamic planning exercise)
- STC 纯硬件自动下载电路 V2
- 上海内推 | 上海期智研究院于洋老师课题组招收全职研究员
- AHB2APB_bridge 设计
猜你喜欢

Summary 111111111111111111111

In 2021, the global ceramic substrate revenue will be about US $409.7 million, and it is expected to reach US $657.3 million in 2028

Modelarts second job

何恺明团队的“视频版本MAE”,高效视频预训练!Mask Ratio高达90%时效果也很好!...

ICML 2022 𞓜 rethinking anomaly detection based on structured data: what kind of graph neural network do we need

Two minutes to show you the charging standard of the Sub Ledger System

Hanging memory recursive dynamic programming (with example explanation POJ 1163)

moderlarts第二次作业

27. this指向问题

RTL arbiter design
随机推荐
Power supply anti reverse connection and anti backflow - use MOS tube and op amp to realize ideal diode
Modelarts second job
27. this pointing problem
Tensorflow --- creation and reading of tfrecord file
moderlarts第一次培訓
2022-2028 global and Chinese thermopile detector Market Status and future development trend
A brief talk on shutter button
This article introduces you to the infrastructure of sofaregistry
The latest test questions and answers for the eight major members (standard members) of Ningxia architecture in 2022
AHB_ Bus_ Matrix_ 3x3 design
Ora-01089 ora-19809 ora-19815 exceeded the limit for recovering files
Full list! 2022 Times Asia University Rankings announced!
上海内推 | 上海期智研究院于洋老师课题组招收全职研究员
Database introduction
Interface isolation principle
统一异常处理
QQ贴吧那种图片一点开,就变了的原理
RTL arbiter design
C deep copy
Flutter doctor 显示xcode没有安装的解决办法