zoukankan      html  css  js  c++  java
  • Leetcode6.ZigZag ConversionZ字形变换

    将字符串 "PAYPALISHIRING" 以Z字形排列成给定的行数:

    之后从左往右,逐行读取字符:"PAHNAPLSIIGYIR"

    实现一个将字符串进行指定行数变换的函数:

    string convert(string s, int numRows);

    示例 1:

    输入: s = "PAYPALISHIRING", numRows = 3 输出: "PAHNAPLSIIGYIR"

    示例 2:

    输入: s = "PAYPALISHIRING", numRows = 4 输出: "PINALSIGYAHRPI"

    解释:

    class Solution {
    public:
        string convert(string s, int numRows)
        {
            int len = s.size();
            if(len == 0)
                return "";
            if(numRows == 1)
                return s;
            vector<vector<char> > v(len, vector<char>(len, 0));
            int i = 0, j = 0;
            int cnt = 0;
            while(cnt < len)
            {
                if(j % (numRows - 1) == 0)
                {
                    for(i = 0; i < numRows && cnt < len; i++)
                    {
                        v[i][j] = s[cnt++];
                    }
                    j++;
                    i--;
                }
                else
                {
                    v[--i][j++] = s[cnt++];
                }
            }
            string res = "";
            for(int i = 0; i < len; i++)
            {
                for(int j = 0; j < len; j++)
                {
                    if(v[i][j] != 0)
                        res += v[i][j];
                }
            }
            return res;
        }
    };

  • 相关阅读:
    【瞎口胡】基础数学 1 快速幂 整除 最大公约数
    【瞎口胡】二分图
    Windos下使用Redis
    VUE的踩坑日记(1)
    公告
    [维度打击]最大连续子序列
    常用函数
    树状数组
    高精度封装
    T4 模板之 单个文件
  • 原文地址:https://www.cnblogs.com/lMonster81/p/10433883.html
Copyright © 2011-2022 走看看