刷LeetCode(6)——ZigZag Conversion

刷LeetCode(6)——ZigZag Conversion

Code it now!https://leetcode.com/problems/zigzag-conversion/description/
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)

P   A   H   N
A P L S I I G
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”.

翻译:

字符串“PAYPALISHIRING”通过一个给定的行数写成如下这种Z型模式:
P   A   H   N
A P L S I I G
Y   I   R

然后一行一行的读取:“PAHNAPLSIIGYIR”

写代码读入一个字符串并通过给定的行数做这个转换:

string convert(string text, int nRows);

调用convert("PAYPALISHIRING", 3),应该返回"PAHNAPLSIIGYIR"

如果还是没明白题目的意思,看下图吧……

这里写图片描述

解法一:直接暴力解决,实现很简单,但是效率不是很高。

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    string convert(string& s, int numRows) {
        string result;
        int orientation = 0;   // 0 ---> down , 1 ---> up
        int i = 0 , j = 0 ,index = 0;
        const int len = s.length();
        if( !len ){
            return result;
        }
        if( numRows == 1 )
        {
            return s;
        }

        vector< vector<char> > vec(numRows,vector<char>(len,0));

        while( index < len )
        {
            // up --- > down 
            if( !orientation )
            {
                vec[i++][j] = s.at(index++);
                if( i == numRows )
                {
                    orientation = 1;
                    i -= 2;
                    j++;
                }
            }
            // down --> up 
            else
            {
                vec[i--][j] = s.at(index++);
                if( i < 0 )
                {
                    orientation = 0;
                    i = 1;
                }
                else
                {
                    j++;
                }
            }
        }

        for( i = 0;i<numRows;++i)
        {
            for(j=0;j<len;++j)
            {
                if( vec[i][j] != 0 )
                {
                    result.push_back(vec[i][j]);
                    if( result.length() == len ){
                        break;
                    }
                }

            }
        }
        return result;
    }
};


int main()
{
    string str;
    vector <string> v;

    while( getline(cin,str) &&  !str.empty() )
    {
        v.push_back(str);
    }

    for( vector<string>::iterator iter = v.begin(); iter != v.end(); ++iter)
    {
        string result = Solution().convert(*iter,3);

        cout<< " result = " << result << endl;
    }
    return 0;
} 

解法二: 每一行对应一个字符串,遍历原字符串,然后把相应的字符加到对应行的字符串。

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    string convert(string& s, int numRows) {
        string result = "";
        vector<string> rowString(numRows,"");
        // 1 ---> up to down . -1 ----> down to up 
        int rowIndex = 0,orientation = 1;

        const int len = s.length();

        if( numRows == 1 || numRows > len )
        {
            return s;
        }

        for( int i=0;i<len;++i)
        {
            rowString[rowIndex] += s.at(i);
            if( rowIndex == 0 )
            {
                // up to down 
                orientation = 1;
            }

            if( rowIndex == numRows -1 )
            {
                // down to up 
                orientation = -1;
            }

            rowIndex += orientation;
        }
        for( int i = 0;i<numRows;++i)
        {
            result += rowString.at(i);
        }

        return result;
    }
};


int main()
{
    string str;
    vector <string> v;

    while( getline(cin,str) &&  !str.empty() )
    {
        v.push_back(str);
    }

    for( vector<string>::iterator iter = v.begin(); iter != v.end(); ++iter)
    {
        string result = Solution().convert(*iter,3);

        cout<< " result = " << result << endl;
    }
    return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值