Leetcode 6. ZigZag Conversion

2 solutions - Sort by Row & Visit by Row

Solution 1 Sort by Row
Initialize a list contains numRows rows. Iterate through s from left to right and add the character into appropriate row in the list. Combime the characters in each row together.
Use curRow and direction to know which row is the right one for current character.

class Solution {
    public String convert(String s, int numRows) {
        if(s.length()==1||numRows<2){
            return s;
        }
        List<StringBuilder> rows = new ArrayList<>();
        for (int i = 0; i < numRows; i++) {
            rows.add(new StringBuilder());
        }//initialize
        int dir=0;//direction:down or up
        int curRow=0;
        for(int j=0;j<s.length();j++){
            rows.get(curRow).append(s.charAt(j));
            if(dir==0){
                curRow+=1;
            }
            else{
                curRow-=1;
            }
            if(curRow==numRows-1||curRow==0){
                dir=~dir;
            }
        }
        StringBuilder res = new StringBuilder();
        for (StringBuilder row : rows) {
            res.append(row);
        }
        return res.toString();

    }
}

Complexity Analysis

Time Complexity: O(n) where n is the length of s
Space Complexity: O(n)

Solution 2 Visit by Row
Visit the characters in the same order as reading the Zig-Zag pattern line by line.
在这里插入图片描述

class Solution {
    public String convert(String s, int numRows) {
    if (s.length() == 1 || numRows < 2) {
        return s;
    }

    StringBuilder res = new StringBuilder();
    int len = s.length();
    
    int cycle = 2 * numRows - 2;

    for (int i = 0; i < numRows; i ++) {
        for (int j = i; j < len; j += cycle) {
            res.append(s.charAt(j));
            if (i > 0 && i < numRows - 1) {//if not the first row and the last row
                int k = j + cycle - 2 * i;
                if (k < len) {
                    res.append(s.charAt(k));
                }
            }
        }
    }
    return res.toString();
    }
}

Complexity Analysis

Time Complexity: O(n) where n is the length of s. Each index is visited once.
Space Complexity: O(n). For the cpp implementation, O(1) if return string is not considered extra space.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值