当前位置:网站首页>6 zigzag conversion of leetcode
6 zigzag conversion of leetcode
2022-07-27 05:23:00 【Master yuan】
List of articles
1.Description
The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: “PAHNAPLSIIGYIR”
Write the code that will take a string and make this conversion given a number of rows:
string convert(string s, int numRows);
2. TC
Example 1:
Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"
Example 2:
Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:
P I N
A L S I G
Y A H R
P I
Example 3:
Input: s = "A", numRows = 1
Output: "A"
3.Solution
public String convert(String s, int numRows) {
if(numRows == 1)
return s;
List<StringBuilder> ans = new ArrayList<>();
for (int i = 0; i <= Math.min(numRows, s.length()); i++) {
ans.add(new StringBuilder());
}
boolean goingDown = false;
int curRow = 0;
for(char c : s.toCharArray()) {
ans.get(curRow).append(c);
if (curRow == 0 || curRow == numRows -1)
goingDown = !goingDown;
curRow += goingDown ? 1 : -1;
}
StringBuilder sb = new StringBuilder();
for(StringBuilder chars: ans){
sb.append(chars);
}
return sb.toString();
}
Ideas :
Then use one list Store the contents of each line , The content of each line is incoherent , So with StringBuilder To concatenate strings .
Then decide which line to go in according to the direction of going up or down again .
Finally, put all StringBuilder Spliced into a complete string .
public String convert(String s, int numRows) {
if(numRows == 1)
return s;
StringBuilder sb = new StringBuilder();
int n = s.length();
int df = 2*numRows -2;
for (int i = 0; i < numRows; i++) {
for (int j = 0; j + i < n; j+=df ) {
sb.append(s.charAt(i + j));
if (i != 0 && i != numRows - 1 && j -i +df < n) {
sb.append(s.charAt(j - i + df));
}
}
}
return sb.toString();
}
Read line by line Z The content of the font , To calculate the difference between two points , Find the law .
P I N
A L S I G
Y A H R
P I
Pictured above is an example , Three characters in the first line P, I, N, The subscripts in the original string are 0, 6, 12, The difference is 6, This value is related to the number of rows ,3 Line difference is 4, The rule is 2* Row number -2;
For the number of rows in the middle , Namely j-i+df
边栏推荐
猜你喜欢

Scientific Computing Library - numpy

The project connects with Alipay payment, and the intranet penetration realizes the monitoring of asynchronous callback notification of successful payment of Alipay

2021 OWASP top 6-10 collection

Detailed description of binary search tree

Explore the mysteries of the security, intelligence and performance of the universal altek platform!

JVM上篇:内存与垃圾回收篇十--运行时数据区-直接内存

Laozi cloud and Fuxin Kunpeng achieved a major breakthrough in 3D ofd 3D format documents for the first time

Acticiti中startProcessInstanceByKey方法在variable表中的如何存储

辗转相除法

File processing (IO)
随机推荐
简化JDBC的MyBits框架
笔记系列k8s编排MySQL容器-有状态的容器创建过程
Database design - relational data theory (ultra detailed)
JVM上篇:内存与垃圾回收篇五--运行时数据区-虚拟机栈
2021 OWASP top 5: security configuration error
JVM Part 1: memory and garbage collection part 9 - runtime data area - object instantiation, memory layout and access location
JVM Part 1: memory and garbage collection part 11 -- execution engine
B1030 完美数列
内部类与静态内部类区别及举例
数据库设计——关系数据理论(超详细)
Create datasource using Druid connection pool
JVM上篇:内存与垃圾回收篇八--运行时数据区-方法区
Typescript details
redis发布订阅模式
秒杀系统设计
Laozi cloud and Fuxin Kunpeng achieved a major breakthrough in 3D ofd 3D format documents for the first time
JVM上篇:内存与垃圾回收篇十二--StringTable
B1031 查验身份证
pytorch中几个难理解的方法整理--gather&squeeze&unsqueeze
接收方设置并发量和限流