LeetCode刷题之旅(6)

来源:互联网 发布:阿里云素材库 编辑:程序博客网 时间:2024/05/04 16:37

题目描述

把握计数变量的循环即可,解决代码如下:

public class Solution {    public String convert(String s, int numRows) {        if (numRows == 1 || s.length() <= 1) {            return s;        }        String[] strings = new String[numRows];        for (int i = 0; i < numRows; i++) {            strings[i] = "";        }        int n = s.length();        int j = 0;        String result = "";        boolean add = true;        for (int i = 0; i < n; i++) {            strings[j] += s.charAt(i);            if (j == numRows - 1) {                add = false;            }            if (add) {                j++;            }else{                j--;            }            if (j == 0 && !add) {                add = true;            }        }        for (String string : strings) {            result += string;            System.out.println(string);        }        return result;    }}
0 0
原创粉丝点击