6Z字形变换

自己的方法有点像题解方法2但是比他复杂很多,题解方法1的变化方向的思想很巧妙

#include<iostream>
#include<string>
#include<vector>
using namespace std;
//很蠢的二维数组做法,击败了15%的人
class Solution {
public:
    string convert(string s, int numRows) {
        if (numRows == 1)
        {
            return s;
        }
        int n = 2*numRows-2 ;//n为每组数目
        int m = s.size();
        int left = m % n;
        int columns;//列
        if (left == 0)
        {
            columns=(numRows-1)*( m / n );
        }
        else
        {
            if(left<numRows)
            {
                columns = (numRows - 1) * (m / n)+1;
            }
            else
            {
                columns = (numRows - 1) * (m / n) + 1 + left - numRows;
            }
        }
            
        char** a = new char*[numRows];//二维char数组的创建
        for (int i = 0; i < numRows; i++)
        {
            a[i] = new char[columns];
            for (int j = 0; j < columns; j++)
            {
                a[i][j] = ' ';
            }
        }
        int index = 0;
        for (int i = 0; i < columns; i++)
        {     
            if (i % (numRows-1) == 0 )//正常列
            {
                for (int j = 0; j < numRows && index < s.size(); j++)
                {
                    a[j][i] = s[index];
                    index++;
                }
            }
            else if(index<s.size())
            {
                int r = i % (numRows - 1);
                a[numRows-1-r][i] = s[index];
                index++;
            }       
        }
        /*for (int i = 0; i < numRows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                cout << a[i][j];
            }
            cout << endl;
        }*/
        string l="";
        int index1 = 0;
        for (int i = 0 ; i < numRows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                if (a[i][j] != ' ')
                {
                    l+= a[i][j];
                }
            }
        }
        return l;
    }
};
//题解方法1
class Solution1 {
public:
    string convert(string s, int numRows) {
        if (numRows == 1)return s;
        vector<string>rows(min(numRows, int(s.size())));
        int curRow = 0;
        bool goingDown = false;
        for (char c : s)//C++中的遍历字符串
        {
            rows[curRow] += c;
            if (curRow == 0 || curRow == numRows - 1)
            {
                goingDown = !goingDown;//移动到最上面或最下面的行的时候,当前方向会发生改变
            }
            curRow += goingDown ? 1 : -1;//? :优先级更高,向上行数加1,向下行数-1
        }
        string ret; //作为返回值的字符串
        for (string row : rows)ret += row;//一行一行的输出
        return ret;
    }
};
int main()
{
    Solution1 test;
    string s = "PAHNAPLSIIGYIR";
    int numRows = 4;
    string l = test.convert(s, numRows);
    for (int i = 0; i < l.size(); i++)
    {
        cout << l[i];
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值