力扣刷题:6、Z字形变换

题目要求

在这里插入图片描述

  • 1 <= s.length <= 1000
  • s 由英文字母(小写和大写)、’,’ 和 ‘.’ 组成
  • 1 <= numRows <= 1000

原题链接

代码

class Solution {
public:
    string convert(string s, int numRows) {
        if (numRows == 1)
        {
            return s;
        }
        int interval = numRows - 2;
        vector<string> strV(numRows);
        for (size_t i = 0; i < s.length(); i++)
        {
            int index = i % (numRows + interval);
            if (index < numRows)
            {
                strV[index] += s[i];
            }
            else
            {
                index = index - numRows;
                strV[strV.size() - index - 2] += s[i];
            }
        }
        for (auto i : strV)
            cout << i << ends;
        string resultStr;
        for (auto i : strV)
            resultStr += i;
        return resultStr;
        
    }
};

整体思路

这道题最主要的是观察字符串和变型模式,找到模式的规律后,剩下的就迎刃而解了。
给了一个字符串和一个行数。
字符串里面的字符按照组来划分。每组有numRows+(numRows - 2)个字符。
每一组中,前numRows个按照正常顺序放进对应编号的容器中。后面numRows-2个字符是Z字形两个竖直之间的斜线。从倒数第二行开始倒序往前排。

学到了什么

1、字符串的处理…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值