[LeetCode] ZigZag Conversion

zigzag模式字符串转换算法
本文介绍了一种将给定字符串转换为指定行数的zigzag模式的方法,并提供了两种实现方式,一种是直接操作字符串,另一种是使用二维数组。重点讨论了zigzag模式的形成规律及其在实际应用中的价值。

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)

 0123456
0P A H N
1APLSIIG
2Y I R  

P A H N
APLSIIG
Y I R  

And then read line by line: "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"

Idea:  通过找规律,可以知道一个zigzag 有 2 * nRows -2 个元素。 

先放第一排和最后一排的元素。

在放之字形中间的元素


class Solution {
public:
    string convert(string s, int nRows) {
        int size = s.size();
        if(size <= 1 || nRows <= 1 || size <= nRows) return s;
        
        string result ;
        
        int k = 0,
            zigzag = 2 *nRows - 2, //one zigzag element count
            count = size/zigzag + 1;   //how many zigzag
        for(int i = 0; i < nRows; i++)
        {
            for(int j = i; j < size; j+=zigzag)
            {
                result.append(1, s[j]); // first and last
                if(i != 0 && i!= nRows -1 && j + zigzag - 2 *i < size)
                {
                    result.append(1, s[j + zigzag - 2 * i]);
                }
            }
        }
        
        return result;
    }
};



Idea: 将一维数组string[size] 转为二维数组result[nRows][ ], 然后按照行输出。

 --------------------------------Memory Limit Exceeded------------------------------------------

class Solution {
public:
    string convert(string s, int nRows) {
        int size = s.size();
        if(size <= 1 || nRows <= 1 || size <= nRows) return s;
        
        int **p = new int* [nRows];
        int nCols = (size/(2*nRows - 2) + 1)*(nRows -1);
        for(int i = 0; i < nRows; i++)
        {
            p[i] = new int [nCols];
        }
        
        int k = 0,
            row = nRows - 1;
        for(int j = 0; j < nCols; j++)
        {
            for(int i = 0; i < nRows; i++)
            {
                if(!(j % row) && k < size)
                {
                    p[i][j] = s[k++];
                }
                else if(!(i + j % row) && k < size)
                {
                    p[i][j] = s[k++];
                }
                else
                {
                    p[i][j] = ',';
                }
            }
        }
        
        k = 0;
        for(int i = 0; i < nRows; i++)
        {
            for(int j = 0; j < nCols; j++)
            {
                if(p[i][j] != ',')
                {
                    s[k++] = p[i][j];
                    
                }
            }
        }
        
        for(int i = 0; i < nRows; i++)
        {
            delete [] p[i];
        }
        
        delete [] p;
        
        return s;
    }
};






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值