当前位置:网站首页>3.1 leetcode daily question 6
3.1 leetcode daily question 6
2022-07-26 10:18:00 【Fabulouskkk】

The first feeling is to use a two-dimensional array to store each character , It's good to find a rule, but it doesn't seem to find it …
| character | Indexes |
|---|---|
| P | 0 |
| A | 1 |
| Y | 2 |
| P | 3 |
| A | 4 |
| L | 5 |
| I | 6 |
| S | 7 |
| H | 8 |
| I | 9 |
| R | 10 |
| I | 11 |
| N | 12 |
| G | 13 |
If numRows=4
The first 0 That's ok -> P - I - N
Corresponding index -> 0 - 6 -12
spacing -> 6 - 6
The first 1 That's ok -> A - L - S - I - G
Corresponding index -> 1 - 5 - 7 - 11 - 13
spacing -> 4 - 2 - 4 - 2
The first 2 That's ok -> Y - A - H - R
Corresponding index -> 2 - 4 - 8 - 10
spacing -> 2 - 4 - 2
The first 3 That's ok -> P - I
Corresponding index -> 3 - 9
spacing -> 6
It's not hard to see. , The rule is in the spacing , set up step = 2*numRows - 2, When numRows = 4 when ,step = 6
And the index corresponding to the first element of each row is the row number
The spacing between the elements in the first and last lines is step
The spacing of elements in other lines is step-2* Row number And 2* Row number In exchange for
public static String convert(String s, int numRows) {
if (numRows == 1) {
return s;
}
List<StringBuilder> rows = new ArrayList<>();
// stay List Add... To the collection numRows individual StringBuilder object
for (int i = 0; i < numRows; i++) {
rows.add(new StringBuilder());
}
int step = 2 * numRows - 2;
for (int i = 0; i < numRows; i++) {
int index = i;
int add = i * 2;
if (i == 0) {
while (index < s.length()) {
rows.get(i).append(s.charAt(index));
index += step;
}
} else if (i == numRows - 1) {
while (index < s.length()) {
rows.get(i).append(s.charAt(index));
index += step;
}
} else {
while (index < s.length()) {
rows.get(i).append(s.charAt(index));
add = step - add;
index = index + add;
}
}
}
StringBuilder res = new StringBuilder();
for (StringBuilder row : rows) {
res.append(row);
}
return res.toString();
}
Then I saw the thought of a big man , Because the characters turn on the zero line and the last line , Let's play one. flag Enable it to turn 
public static String convert(String s, int numRows) {
if (numRows == 1) {
return s;
}
List<StringBuilder> rows = new ArrayList<>();
for (int i = 0; i < numRows; i++) {
rows.add(new StringBuilder());
}
int i = 0;
int flag = -1;
for (char c : s.toCharArray()) {
rows.get(i).append(c);
if (i == 0 || i == numRows - 1) {
flag = -flag;
}
i = i + flag;
}
StringBuilder res = new StringBuilder();
for (StringBuilder row : rows) {
res.append(row);
}
return res.toString();
}
边栏推荐
- Show default image when wechat applet image cannot be displayed
- 30分钟彻底弄懂 synchronized 锁升级过程
- Beginner of flask framework-04-flask blueprint and code separation
- 服务发现原理分析与源码解读
- 面试突击68:为什么 TCP 需要 3 次握手?
- [Qualcomm][Network] qti服务分析
- Uniapp common error [wxml file compilation error]./pages/home/home Wxml and using MySQL front provided by phpstudy to establish an independent MySQL database and a detailed tutorial for independent da
- Learning about opencv (4)
- 时间序列异常检测
- Redis realizes distributed lock and gets a watchdog
猜你喜欢

INSTALL_ FAILED_ SHARED_ USER_ Incompatible error resolution

30 minutes to thoroughly understand the synchronized lock upgrade process

Flask框架初学-03-模板

【Halcon视觉】图像滤波

Learning about opencv (4)

【Halcon视觉】图像灰度变化

Uni app learning summary

Wechat applet learning notes 1

Flask framework beginner-03-template

面试突击68:为什么 TCP 需要 3 次握手?
随机推荐
Uni app learning summary
Yarn 'TSC' is not an internal or external command, nor is it a runnable program or batch file. The problem that the command cannot be found after installing the global package
Applet record
AirTest
Vs2019 configuring opencv
Data communication foundation telnet remote management equipment
Okaleido ecological core equity Oka, all in fusion mining mode
Android greendao数据库的使用
【Halcon视觉】阈值分割
Encapsulation of tabbarcontroller
Use of selectors
如何写一篇百万阅读量的文章
Use of Android grendao database
[qualcomm][network] QTI service analysis
【Halcon视觉】形态学膨胀
Basic usage of protobuf
Tableviewcell highly adaptive
Show default image when wechat applet image cannot be displayed
Uniapp common error [wxml file compilation error]./pages/home/home Wxml and using MySQL front provided by phpstudy to establish an independent MySQL database and a detailed tutorial for independent da
数通基础-TCPIP参考模型