6——Z 字形变换(ZigZag Conversion)

题目描述

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

比如输入字符串为 “LEETCODEISHIRING” 行数为 3 时,排列如下:

L   C   I   R
E T O E S I I G
E   D   H   N

之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:“LCIRETOESIIGEDHN”。

请你实现这个将字符串进行指定行数变换的函数:

string convert(string s, int numRows);

示例1

输入: s = "LEETCODEISHIRING", numRows = 3
输出: "LCIRETOESIIGEDHN"

示例2

输入: s = "LEETCODEISHIRING", numRows = 4
输出: "LDREOEIIECIHNTSG"
解释:

L     D     R
E   O E   I I
E C   I H   N
T     S     G

例题解法:

因为我自己写的将近 100 行代码,实在不能看,所以就去看了解析,下面是解析的解法。
由题我们可知一般情况下输出的行数就是传入的 numRows,而当传入的字符串字符数小于 numRows 时,行数即为该字符串所包含的字符数,所以直接用 n(行数)个 StringBuilder 来存储每行的字符,最后再将它们连接到一起即我们要输出的结果。(真是巧妙啊这个方法)
尤其要注意当字符数少于需要输出的行数这种情况。

class Solution {
    public String convert(String s, int numRows) {
        if (numRows == 1) {
            return s;
        }
        ArrayList<StringBuilder> arrs = new ArrayList<>();
        for (int i = 0; i < Math.min(numRows, s.length()); i++) {
            arrs.add(new StringBuilder());
        }
        boolean direction = true;
        int cur_row = 0;
        for (int i = 0; i < s.length(); i++) {
        	arrs.get(cur_row).append(s.charAt(i));
        	cur_row = cur_row + (direction ? 1 : -1);
            if (cur_row == 0 || cur_row == numRows - 1) direction = !direction;
        }
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < arrs.size(); i++) {
        	result.append(arrs.get(i));
        }
        return result.toString();
    }
}

自己的憨憨解法

是真的菜,还想先去计算了数组大小,然后再数组一个一个添加,实属憨憨写法,只有我自己能看懂。

class Solution {
    public String convert(String s, int numRows) {
        int n = numRows;
        String result = "";
		int len = s.length();
		int column = 0;
		if (n == 1) {
			column = len;
            result += s;
		} else if (n == 2) {
			column = len/2 + len%2;
            for (int i=0, j=0; i < column; i++, j+=2) {
                result += s.charAt(j);
            }
            if (len%2 == 1) {
                for (int i=0, j=1; i < column-1; i++, j+=2) {
                    result += s.charAt(j);
                }
            } else {
                for (int i=0, j=1; i < column; i++, j+=2) {
                    result += s.charAt(j);
                }
            }
		} else {
			int length = len;
			boolean a = true;
			int l_str = 0, s_str = 0;
			while (length != 0) {
				if (a) {
					l_str++;
					if (l_str == n) {
						a = false;
						l_str = 0;
						column++;
					}
				} else {
					s_str++;
					if (s_str == (n-2)) {
						a = true;
						s_str = 0;
					}
					column++;
				}
				length--;
			}
			if (l_str>0 && l_str<n) {
				column++;
			}
            char[][] z = new char[n][column];
            for (int m = 0; m < n; m++) {
		    	for (int k =0; k < column; k++) {
			    	z[m][k] = 32;
			    }
		    }
            ArrayList<Character> arr = new ArrayList<>();
            for (int i = 0; i < len; i++) {
                arr.add(s.charAt(i));
            }

            int long_str_count = 0;		//max is n
            int single_char_count = 0;	//max is 3n-2
            boolean flag = true;
            int i = 0, j = 0;
            while (true) {
                if (flag) {
                    z[i][j] = arr.get(0);
                    arr.remove(0);
                    long_str_count++;
                    if (arr.isEmpty()) {
                        break;
                    }
                    if (long_str_count==n) {
                        flag = false;
                        long_str_count = 0;
                        i--;
                        j++;
                    } else {
					    i++;
				    }
                } else {
                    z[i][j] = arr.get(0);
                    arr.remove(0);
                    single_char_count++;
                    if (arr.isEmpty()) {
                        break;
                    }
                    if (single_char_count == (n-2)) {
                        flag = true;
                        single_char_count = 0;
                    } else {
                    }
                    j++;
                    i--;
                }
            }
            for (int m = 0; m < n; m++) {
                for (int k =0; k < column; k++) {
                    if (z[m][k] != 32) {
                        result += z[m][k];
                    }
                }
            }
		}
        return result;
    }
}

获得知识

StringBuilder 比 String 执行更快,因为使用 String 进行连接时,每次都会创建一个新的对象,而 StingBuilder 不会。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值