当前位置:网站首页>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();
}
边栏推荐
- Error in render: "typeerror: cannot read properties of undefined (reading 'length')" --- error when calling interface
- regular expression
- RecyclerView最后一条显示不全或显示部分的问题解决
- Reproduce the snake game in C language (I) build pages and construct snakes
- Data communication foundation - layer 2 switching principle
- Installation and use of cocoapods
- PLC概述
- 简单化构造函数的继承方法(二)- ES6中的class继承
- On the compilation of student management system of C language course (simple version)
- 数据库的复习--3.SQL语言
猜你喜欢
随机推荐
面试突击68:为什么 TCP 需要 3 次握手?
Learning about opencv (2)
Flask框架初学-03-模板
Rocky basic exercise -shell script 2
Review of database -- 3. SQL language
INSTALL_ FAILED_ SHARED_ USER_ Incompatible error resolution
面试第二家公司的面试题及答案(二)
Netease cloud UI imitation -- & gt; sidebar
Leetcode 504. 七进制数
AirTest
PHP one-time request lifecycle
In Net 6.0
C language course design Tetris (Part 1)
Vs2019 configuring opencv
Mysql5.7.25 master-slave replication (one-way)
Opencv image processing
数据库的复习--1.概述
Solution of inputting whole line string after inputting integer
Usage of the formatter attribute of El table
PHP executes shell script









