6.ZigZag(leetcode)

原题地址:
https://leetcode-cn.com/problems/zigzag-conversion/description/

将字符串 “PAYPALISHIRING” 以Z字形排列成给定的行数:

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

之后从左往右,逐行读取字符:”PAHNAPLSIIGYIR”

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

string convert(string s, int numRows);

示例 1:

输入: s = “PAYPALISHIRING”, numRows = 3
输出: “PAHNAPLSIIGYIR”

示例 2:

输入: s = “PAYPALISHIRING”, numRows = 4
输出: “PINALSIGYAHRPI”

解释:

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

解法,用二维数组来弄,计算下标有点绕,还好
java版本:

import java.util.*;
import java.lang.*;

class Program
{


    public static String convert(String s, int nRows) {
    if (nRows == 1)
        return s;

    // each unit
    int amountInUnit = nRows + nRows - 2;
    int totalUnits = s.length()/ amountInUnit;
    if (s.length() % amountInUnit != 0)
        totalUnits++;

    // each unit is a rectangle
    int rows = nRows;
    int cols = totalUnits * (nRows - 1);
    char[][] map = new char[rows][cols];

    int i = 0;
    while (i < s.length()) {
        char ch = s.charAt(i);

        // which unit, starts from 0
        int unitNumber = i / amountInUnit;

        // which postion in the unit, starts from 0
        int posInUnit = i % (amountInUnit);

        // if it's in the first column of the unit
        int x, y;
        if (posInUnit < nRows) {
            x = posInUnit;
            y = unitNumber * (nRows - 1);
        } else {
            x = nRows - 1 - (posInUnit + 1 - nRows);
            y = nRows - x - 1 + unitNumber * (nRows - 1);
        }
        map[x][y] = ch;

        i++;

    } // while

    // iterate and output
    StringBuilder sb = new StringBuilder();
    for (i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            if (map[i][j] != 0)
                sb.append(map[i][j]);
        }
    }
    return sb.toString();
}

    public static void main(String[] args)
    {
        System.out.println(convert("PAYPALISHIRING", 4));//"PINALSIGYAHRPI"
        System.out.println(convert("PAYPALISHIRING", 3));//"PAHNAPLSIIGYIR"
        System.out.println();



    }
}

C#版本

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace zigzag
{
    class Program
    {


        public static String convert(String s, int nRows) {
        if (nRows == 1)
            return s;

        // each unit
        int amountInUnit = nRows + nRows - 2;
        int totalUnits = s.Length/ amountInUnit;
        if (s.Length % amountInUnit != 0)
            totalUnits++;

        // each unit is a rectangle
        int rows = nRows;
        int cols = totalUnits * (nRows - 1);
        char[,] map = new char[rows,cols];

        int i = 0;
        while (i < s.Length) {
            char ch = s[i];

            // which unit, starts from 0
            int unitNumber = i / amountInUnit;

            // which postion in the unit, starts from 0
            int posInUnit = i % (amountInUnit);

            // if it's in the first column of the unit
            int x, y;
            if (posInUnit < nRows) {
                x = posInUnit;
                y = unitNumber * (nRows - 1);
            } else {
                x = nRows - 1 - (posInUnit + 1 - nRows);
                y = nRows - x - 1 + unitNumber * (nRows - 1);
            }
            map[x,y] = ch;

            i++;

        } // while

        // iterate and output
        StringBuilder sb = new StringBuilder();
        for (i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (map[i,j] != 0)
                    sb.Append(map[i,j]);
            }
        }
        return sb.ToString();
    }

        static void Main(string[] args)
        {
            Console.WriteLine(convert("PAYPALISHIRING", 4));//"PINALSIGYAHRPI"
            Console.WriteLine(convert("PAYPALISHIRING", 3));//"PAHNAPLSIIGYIR"
            Console.ReadKey();



        }
    }
}

猜你喜欢

转载自blog.csdn.net/u014028392/article/details/80482165
今日推荐