C语言中如何实现长尾词的Z字形变换?

2026-04-12 08:111阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计597个文字,预计阅读时间需要3分钟。

C语言中如何实现长尾词的Z字形变换?

目录 + Z字形变换 + 描述 + Z字形变换 + 描述 + 将一个给定的字符串s + 根据给定的行数numRows + 从上往下、从左到右进行Z+字形排列。+ 例如,输入字符串为+ PAYPALISHIRING + 行数为+ 3 + ,排列如下:+ P+

目录
  • Z字形变换
    • 描述

Z字形变换

描述

将一个给定字符串 s 根据给定的行数 numRows ,以从上往下、从左到右进行 Z 字形排列。

比如输入字符串为 “PAYPALISHIRING” 行数为 3 时,排列如下:

P A H N A P L S I I G Y I R

之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:“PAHNAPLSIIGYIR”。

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

string convert(string s, int numRows);

示例1

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

示例2

输入:s = "PAYPALISHIRING", numRows = 4
输出:"PINALSIGYAHRPI"
解释:
P I N
A L S I G
Y A H R
P I

示例3

输入:s = "A", numRows = 1
输出:"A"

C语言中如何实现长尾词的Z字形变换?

思路/解法

模拟法,根据所给条件,线性处理即可(Z字形存在一定规律,每当固定的条件后前进方向进行转变)。

class Solution { public: string convert(string s, int numRows) { int rows = numRows; int columns = ((s.length() / (2 * rows - 1)) + 1) * rows;//尽可能缩小所使用的空间,这里columns可优化,并未精确求解 std::vector<std::vector<char>> arrs(rows, std::vector<char>(columns)); //初始化 for (int i = 0; i < rows; i++) for (int j = 0; j < columns; j++) arrs[i][j] = '0'; int x = 0, y = 0; int index = 0; while (index < s.length()) { if (index < s.length() && x < rows) arrs[x++][y] = s[index++]; if (index < s.length() && x == rows) { //更新x和y y++; x -= 2; while (index < s.length() && x > 0) arrs[x--][y++] = s[index++]; x = 0;//重置x } } std::string res; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { if (arrs[i][j] != '0' && arrs[i][j] != '\0') res.push_back(arrs[i][j]); } } return res; } };

到此这篇关于C++中的Z字形变换的文章就介绍到这了,更多相关C++Z字形变换内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

本文共计597个文字,预计阅读时间需要3分钟。

C语言中如何实现长尾词的Z字形变换?

目录 + Z字形变换 + 描述 + Z字形变换 + 描述 + 将一个给定的字符串s + 根据给定的行数numRows + 从上往下、从左到右进行Z+字形排列。+ 例如,输入字符串为+ PAYPALISHIRING + 行数为+ 3 + ,排列如下:+ P+

目录
  • Z字形变换
    • 描述

Z字形变换

描述

将一个给定字符串 s 根据给定的行数 numRows ,以从上往下、从左到右进行 Z 字形排列。

比如输入字符串为 “PAYPALISHIRING” 行数为 3 时,排列如下:

P A H N A P L S I I G Y I R

之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:“PAHNAPLSIIGYIR”。

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

string convert(string s, int numRows);

示例1

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

示例2

输入:s = "PAYPALISHIRING", numRows = 4
输出:"PINALSIGYAHRPI"
解释:
P I N
A L S I G
Y A H R
P I

示例3

输入:s = "A", numRows = 1
输出:"A"

C语言中如何实现长尾词的Z字形变换?

思路/解法

模拟法,根据所给条件,线性处理即可(Z字形存在一定规律,每当固定的条件后前进方向进行转变)。

class Solution { public: string convert(string s, int numRows) { int rows = numRows; int columns = ((s.length() / (2 * rows - 1)) + 1) * rows;//尽可能缩小所使用的空间,这里columns可优化,并未精确求解 std::vector<std::vector<char>> arrs(rows, std::vector<char>(columns)); //初始化 for (int i = 0; i < rows; i++) for (int j = 0; j < columns; j++) arrs[i][j] = '0'; int x = 0, y = 0; int index = 0; while (index < s.length()) { if (index < s.length() && x < rows) arrs[x++][y] = s[index++]; if (index < s.length() && x == rows) { //更新x和y y++; x -= 2; while (index < s.length() && x > 0) arrs[x--][y++] = s[index++]; x = 0;//重置x } } std::string res; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { if (arrs[i][j] != '0' && arrs[i][j] != '\0') res.push_back(arrs[i][j]); } } return res; } };

到此这篇关于C++中的Z字形变换的文章就介绍到这了,更多相关C++Z字形变换内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!