当前位置:网站首页>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
边栏推荐
- Raspberry pie RTMP streaming local camera image
- Bean's life cycle & dependency injection * dependency auto assembly
- How to store the startprocessinstancebykey method in acticiti in the variable table
- JVM Part 1: memory and garbage collection part 11 -- execution engine
- Derivation and explanation of PBR physical illumination calculation formula
- During its low-level period, this slave edge causes the instruction number to make a corresponding model
- JVM上篇:内存与垃圾回收篇三--运行时数据区-概述及线程
- 枚举类实现单例模式
- 听过最自律的一句话: 那些我难以言表 不作声响
- SSM framework integration
猜你喜欢

Detailed description of polymorphism

JVM Part 1: memory and garbage collection part 14 -- garbage collector

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

JDBC API 详解

idea远程调试debug

How to test the payment process?

JVM上篇:内存与垃圾回收篇十二--StringTable

JVM Part 1: memory and garbage collection part 6 -- runtime data area local method & local method stack

Integrate SSM

A math problem cost the chip giant $500million
随机推荐
Scientific Computing Library -- Matplotlib
Basic operation of vim
JVM上篇:内存与垃圾回收篇五--运行时数据区-虚拟机栈
内部类与静态内部类区别及举例
Could not autowire. No beans of ‘userMapper‘ type found.
B1022 D进制的A+B
How to sinicize the JMeter interface?
数据库连接池&&Druid使用
Detailed description of polymorphism
素数筛选(埃氏筛法,区间筛法,欧拉筛法)
JVM Part 1: memory and garbage collection part 6 -- runtime data area local method & local method stack
35.滚动 scroll
JVM上篇:内存与垃圾回收篇三--运行时数据区-概述及线程
Quoted popular explanation
B1027 打印沙漏
Message reliability processing
JVM Part 1: memory and garbage collection part 9 - runtime data area - object instantiation, memory layout and access location
B1031 check ID card
Niuke sword refers to the path in the offer--jz12 matrix
The difference between strlen and sizeof