LeetCode_6

本文介绍了一种将字符串以Z字形排列并按行读取的算法实现。通过示例说明了如何将输入字符串“PAYPALISHIRING”转换为特定格式的输出“PAHNAPLSIIGYIR”。提供了三种不同的C++代码实现方式。

6. ZigZag Conversion

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

字符串“PAYPALISHIRING”在以下给定数目的行上以Z字形图案书写:(您可能希望以固定字体显示该图案以便更好的易读性)

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

And then read line by line: "PAHNAPLSIIGYIR"

 然后按行读"PAHNAPLSIIGYIR"

Write the code that will take a string and make this conversion given a number of rows:

编写代码,采用一个子字符串,并给予这种转换提供行参数。

string convert(string text, int nRows);

convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".

#include<iostream>
#include<set>
#include<string>
#include<vector>
using namespace std;


class Solution {
public:
    string convert(string s, int numRows) {
        if (s.empty())
            return "";
        
        string result;
        int n = s.size();
        int maxRow = (n /  numRows ) * 2;
        if (numRows == 1 || maxRow == 0) {
            return s;
        }
        char **p = new char*[numRows];
        for (int i = 0; i < numRows; i++)
        {
            p[i] = new char[maxRow];
            memset(p[i], NULL, sizeof(char)*maxRow);
        }
        int *index = new int[numRows];
        memset(index, 0, sizeof(int)*numRows);
        for (int i = 0; i < n; )
        {
            for (int j = 0; j < numRows&&i < n; j++) {
                
                p[j][index[j]] = s[i++];
                index[j]++;
            }
            for (int j = numRows-2; j >0 && i < n; j--)
            {
                
                p[j][index[j]] = s[i++];
                index[j]++;
            }
        }

    
        for (int i = 0; i < numRows; i++)
        {
            for (int j = 0; j < maxRow; j++)
            {
                if (p[i][j] != NULL)
                    result += p[i][j];
                else
                    break;
            }

        }
        
        return result;
    }
};
int main()
{
    Solution solution;

    string s = "A";
    string result = solution.convert(s,2);

    cout << result;
    system("pause");
    return 0;
}

别人的代码

class Solution {
public:
    string convert(string s, int numRows) {
        if(numRows == 1) return s;
        else{
            int row = 0, step = 1;
            string *tmp = new string[numRows];
            string rst;
            for(int i = 0; i < s.length(); ++i){
                if(row == 0)    step = 1;
                else if (row == numRows - 1)    step = -1;
                tmp[row].push_back(s[i]);
                row += step;
            }
            for(int i = 0; i < numRows; ++i){
                rst.append(tmp[i]);
            }
            return rst;
        }
    }
};

 

class Solution {
public:
    string convert(string s, int nRows) {
        if (nRows <= 1) return s;
        string res = "";
        int size = 2 * nRows - 2;
        for (int i = 0; i < nRows; ++i) {
            for (int j = i; j < s.size(); j += size) {
                res += s[j];
                int tmp = j + size - 2 * i;
                if (i != 0 && i != nRows - 1 && tmp < s.size()) res += s[tmp];
            }
        }
        return res;
    }
};

 

转载于:https://www.cnblogs.com/pcxie/p/8138261.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值