当前位置:网站首页>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();
}
边栏推荐
- APB2standard_handshake_bridge 设计
- 2022-2028 current market situation and future development trend of thermopile Pyranometers in the world and China
- ICML 2022 𞓜 rethinking anomaly detection based on structured data: what kind of graph neural network do we need
- Text to speech small software
- MySQL——基本的Select语句
- Hdu3527 (Hangdian) spy problem
- AHB_ Bus_ Matrix_ 3x3 design
- 2022Redis7.0x版本持久化详细讲解
- This article introduces you to the infrastructure of sofaregistry
- 29. location對象
猜你喜欢

WR | 西湖大学鞠峰组微纳塑料污染对人工湿地菌群与脱氮功能的影响

Usage of duck beak wire stripper

Jmeter安装

巴比特 | 元宇宙每日必读:数字藏品二级市场乱象丛生,00后成新韭菜,监管迫在眉睫?...

29. Objet de localisation

7905 and TL431 negative voltage regulator circuit - regulator and floating circuit relative to the positive pole of the power supply

【四川大学】初试复试考研资料分享

Linux环境安装mysql数据库详细教程(含卸载和密码重置过程)

使用flask框架写挡板

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%
随机推荐
Proficient in xmake2
Merge sort divide and conquer
2022-2028 global and Chinese thermopile detector Market Status and future development trend
MySQL - Basic select statement
29. Objet de localisation
上海内推 | 上海期智研究院于洋老师课题组招收全职研究员
AHB_ Bus_ Matrix_ 3x3 design
谷歌提出超强预训练模型CoCa,在ImageNet上微调Top-1准确率达91%!在多个下游任务上SOTA!...
WR | 西湖大学鞠峰组微纳塑料污染对人工湿地菌群与脱氮功能的影响
A brief talk on shutter button
2022-2028 global and Chinese thermal power generation (TEG) module market status and future development trend
29. location對象
C deep copy
Poj1028 web navigation
何恺明团队的“视频版本MAE”,高效视频预训练!Mask Ratio高达90%时效果也很好!...
Full list! 2022 Times Asia University Rankings announced!
Jmeter安装
Richter's substitution principle
Volcano engine, Alibaba cloud and Tencent cloud jointly released the 'ultra low delay' live broadcast technical standard
2022-2028 global and Chinese thermopile infrared sensor market status and future development trend