思路:n/t+1向上取整得了
// @Title: Z 字形变换 (Zigzag Conversion)
// @Author: qisiii
// @Date: 2024-04-11 20:31:03
// @Runtime: 37 ms
// @Memory: 45.5 MB
// @comment: n/t+1向上取整得了
// @flag: WHITE
class Solution {
public String convert(String s, int numRows) {
if(numRows<=1||numRows>=s.length()){
return s;
}
//一个周期耗这么多字符
int t=numRows+numRows-2;
//每个周期占多少列
int c=1+numRows-2;
//一共
int column=((s.length())/t+1)*c;
char[][] result=new char[numRows][column];
char[] chars=s.toCharArray();
int x=0,y=0;
for(int i=0;i<chars.length;i++){
result[x][y]=chars[i];
//在周期内
if(i%t<numRows-1){
x++;
}else{
x--;
y++;
}
}
StringBuffer builder=new StringBuffer();
for(char[] row:result){
for(char cl:row){
if(cl != 0){
builder.append(cl);
}
}
}
return builder.toString();
}
}
思路:实际上根本不需要y,行方向的只需要堆积就好,所以可以使用stringbuilder和list
// @Title: Z 字形变换 (Zigzag Conversion)
// @Author: qisiii
// @Date: 2021-12-27 15:11:16
// @Runtime: 5 ms
// @Memory: 38.8 MB
// @comment: 实际上根本不需要y,行方向的只需要堆积就好,所以可以使用stringbuilder和list
// @flag: GREEN
class Solution {
public String convert(String s, int numRows) {
if (s.length()<=numRows||numRows==1){
return s;
}
List<StringBuilder> rows=new ArrayList<>();
for (int i = 0; i < Math.min(numRows,s.length()); i++) {
rows.add(new StringBuilder());
}
int x=0;
int tou=0,wei=numRows-1;
Boolean xia=false;
for (char c : s.toCharArray()) {
if (x==tou||x==wei){
xia=!xia;
}
rows.get(x).append(c) ;
x+=xia?1:-1;
}
StringBuilder res=new StringBuilder();
for (StringBuilder row : rows) {
res.append(row);
}
return res.toString();
}
}
思路:
// @Title: Z 字形变换 (Zigzag Conversion)
// @Author: qisiii
// @Date: 2024-04-11 20:23:09
// @Runtime: 36 ms
// @Memory: 45.5 MB
// @comment:
// @flag:
class Solution {
public String convert(String s, int numRows) {
if(numRows<=1||numRows>=s.length()){
return s;
}
//一个周期耗这么多字符
int t=numRows+numRows-2;
//每个周期占多少列
int c=1+numRows-2;
//一共
int column=(s.length()+t-1)/t*c;
char[][] result=new char[numRows][column];
char[] chars=s.toCharArray();
int x=0,y=0;
for(int i=0;i<chars.length;i++){
result[x][y]=chars[i];
//在周期内
if(i%t<numRows-1){
x++;
}else{
x--;
y++;
}
}
StringBuffer builder=new StringBuffer();
for(char[] row:result){
for(char cl:row){
if(cl != 0){
builder.append(cl);
}
}
}
return builder.toString();
}
}
思路:
// @Title: Z 字形变换 (Zigzag Conversion)
// @Author: qisiii
// @Date: 2022-03-01 13:05:17
// @Runtime: 47 ms
// @Memory: 43.8 MB
// @comment:
// @flag:
class Solution {
public String convert(String s, int numRows) {
if(numRows<=1){
return s;
}
char[] chars=s.toCharArray();
char[][] zhihou=new char[numRows][chars.length];
int x=0,y=0;
int index=0;
while(index<chars.length){
while(x<numRows&&index<chars.length){
zhihou[x++][y]=chars[index++];
}
x--;
while(x>1&&index<chars.length){
zhihou[--x][++y]=chars[index++];
}
x--;y++;
}
StringBuilder str=new StringBuilder();
for(int i=0;i<numRows;i++){
for(int j=0;j<zhihou[0].length;j++){
if (zhihou[i][j]!='\u0000'){
str.append(zhihou[i][j]);
}
}
}
return str.toString();
}
}
思路:活用list和stringbuilder
// @Title: Z 字形变换 (Zigzag Conversion)
// @Author: qisiii
// @Date: 2022-03-01 13:14:31
// @Runtime: 5 ms
// @Memory: 41.3 MB
// @comment: 活用list和stringbuilder
// @flag: ORANGE
class Solution {
public String convert(String s, int numRows) {
if(numRows<=1){
return s;
}
List<StringBuilder> strs=new ArrayList();
for(int i=0;i<Math.min(numRows,s.length());i++){
strs.add(new StringBuilder());
}
boolean xia=false;
int tou=0,wei=numRows-1;
int x=0;
for(char k:s.toCharArray()){
if(x==tou||x==wei){
xia=!xia;
}
strs.get(x).append(k);
x=xia?x+1:x-1;
}
StringBuilder result=new StringBuilder();
for(StringBuilder sp:strs){
result.append(sp);
}
return result.toString();
}
}