字形变换Java版(力扣)

97 阅读1分钟

字形变换

将一个给定字符串 s 根据给定的行数 numRows ,以从上往下、从左到右进行 Z 字形排列。

比如输入字符串为 “PAYPALISHIRING” 行数为 3 时,排列如下:
在这里插入图片描述
之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:“PAHNAPLSIIGYIR”。
请你实现这个将字符串进行指定行数变换的函数:
string convert(string s, int numRows);

示例 1:
输入:s = “PAYPALISHIRING”, numRows = 3
输出:"PAHNAPLSIIGYIR"

示例 2:
输入:s = “PAYPALISHIRING”, numRows = 4
输出:“PINALSIGYAHRPI”
解释:

在这里插入图片描述
示例 3:
输入:s = “A”, numRows = 1
输出:"A"

提示:
1 <= s.length <= 1000
s 由英文字母(小写和大写)、’,’ 和 ‘.’ 组成
1 <= numRows <= 1000

题意:就是给个字符串,把这个字符串按照Z排列,然后我们把排后的结果按行读取输出结果。

思路:创建numRows个StringBuilder,放在集合中,然后我们便利字符串,便利到当前字符时,判断是属于哪行,然后就把这个字符放到这行的StringBuilder中,然后将每行合到一起,输出结果。

正确代码:

class Solution {
    public String convert(String s, int numRows) {

        if(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 currentLine = 0; //当前所处的行
        int flag=-1; // 是否处于拐点,拐点是行数增加或减少

        for (char c : s.toCharArray()) {
            rows.get(currentLine).append(c);
            if(currentLine==0||currentLine==numRows-1) flag = - flag;
            currentLine += flag;
        }

        StringBuilder ret = new StringBuilder();
        for (StringBuilder row : rows) {
            ret.append(row);
        }

        return ret.toString();


    }
}

完整代码(含测试代码):

package com.Keafmd.February.day24;

import java.util.ArrayList;
import java.util.List;

/**
 * Keafmd
 *
 * @ClassName: ZigzagConversion
 * @Description: 字形变换 https://leetcode-cn.com/problems/zigzag-conversion/
 * @author: 牛哄哄的柯南
 * @Date: 2021-03-24 15:00
 * @Blog: https://keafmd.blog.csdn.net/
 */
public class ZigzagConversion {
    public static void main(String[] args) {
        String  s1 = "PAYPALISHIRING";
        int numRows =3 ;
        Solution03141 solution03141 = new Solution03141();
        String result = solution03141.convert(s1,numRows);
        System.out.println("result = " + result);
    }
}
/*    P   A   H   N
      A P L S I I G
      Y   I   R

      P     I    N
      A   L S  I G
      Y A   H R
      P     I

      */
class Solution03141 {
    public String convert(String s, int numRows) {

        if(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 currentLine = 0; //当前所处的行
        int flag=-1; // 是否处于拐点,拐点是行数增加或减少

        for (char c : s.toCharArray()) {
            rows.get(currentLine).append(c);
            if(currentLine==0||currentLine==numRows-1) flag = - flag;
            currentLine += flag;
        }

        StringBuilder ret = new StringBuilder();
        for (StringBuilder row : rows) {
            ret.append(row);
        }

        return ret.toString();


    }
}

输出结果:

result = PAHNAPLSIIGYIR

Process finished with exit code 0

看完如果对你有帮助,感谢点赞支持!
如果你是电脑端,看到右下角的 “一键三连” 了吗,没错点它[哈哈]

在这里插入图片描述
加油!

共同努力!

Keafmd