当前位置:网站首页>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();
}
边栏推荐
- Flask framework beginner-03-template
- Reproduce the snake game in C language (I) build pages and construct snakes
- Flask框架初学-04-flask蓝图及代码抽离
- 【Halcon视觉】图像的傅里叶变换
- Wechat applet learning notes 2
- Server memory failure prediction can actually do this!
- Docker configuring MySQL Cluster
- 【Halcon视觉】软件编程思路
- PMM (percona monitoring and management) installation record
- Li Kou - binary tree pruning
猜你喜欢
【有奖提问】向图灵奖得主、贝叶斯网络之父 Judea Pearl 提问啦
Basics of data communication - basic knowledge of network
服务发现原理分析与源码解读
Cause: could't make a guess for solution
数通基础-二层交换原理
Data communication foundation telnet remote management equipment
Data communication foundation - layer 2 switching principle
Draw arrows with openlayer
点赞,《新程序员》电子书限时免费领啦!
数通基础-STP原理
随机推荐
Study on the basis of opencv
在.NET 6.0中配置WebHostBuilder
Principle analysis and source code interpretation of service discovery
WARNING: [pool www] server reached pm. max_ children setting (5), consider raising it
Mysql5.7.25 master-slave replication (one-way)
PHP one-time request lifecycle
In Net 6.0
Server memory failure prediction can actually do this!
Study notes of the third week of sophomore year
【Halcon视觉】阈值分割
万字详解“用知识图谱驱动企业业绩增长”
Draw arrows with openlayer
Sublime install plug-ins
Getting started with SQL - combined tables
Reproduce the snake game in C language (I) build pages and construct snakes
SQL优化的魅力!从 30248s 到 0.001s
【有奖提问】向图灵奖得主、贝叶斯网络之父 Judea Pearl 提问啦
Common errors when starting projects in uniapp ---appid
数据库的复习--1.概述
Data communication foundation STP principle