当前位置:网站首页>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
边栏推荐
猜你喜欢

idea远程调试debug

Shell course summary

Detailed description of polymorphism

How idea creates a groovy project (explain in detail with pictures and texts)

JVM Part 1: memory and garbage collection part 9 - runtime data area - object instantiation, memory layout and access location

Idea remote debugging

JVM上篇:内存与垃圾回收篇九--运行时数据区-对象的实例化,内存布局与访问定位

集合框架的使用

实用小工具: Kotlin 代码片段

JVM Part 1: memory and garbage collection part 12 -- stringtable
随机推荐
Database connection pool & Druid usage
MQ FAQ
LeetCode刷题之322 Coin Change
2022 Zhengzhou light industry Freshmen's competition topic - I won't say if I'm killed
牛客剑指offer--JZ12 矩阵中的路径
Li Kou achieved the second largest result
Detailed description of polymorphism
Detailed description of binary search tree
LeetCode之268.Missing number
[optical flow] - data format analysis, flowwarp visualization
JVM part I: memory and garbage collection part II -- class loading subsystem
Gradio quickly builds ml/dl Web Services
The difference between strlen and sizeof
求组合数(最强优化)
李宏毅机器学习组队学习打卡活动day05---网络设计的技巧
Interface and abstract class / method learning demo
SSM framework integration
2022年郑州轻工业新生赛题目-打死我也不说
Critical path principle
枚举类实现单例模式