当前位置:网站首页>LeetCode 6. Z 字形变换 找规律
LeetCode 6. Z 字形变换 找规律
2022-08-04 09:23:00 【超级码力奥】
将一个给定字符串 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”
提示:
1 <= s.length <= 1000
s 由英文字母(小写和大写)、‘,’ 和 ‘.’ 组成
1 <= numRows <= 1000
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/zigzag-conversion
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
找规律, 看看哪一部分应该放在循环之中,下面是我的代码
class Solution {
public:
string convert(string s, int numRows) {
if(numRows == 1) return s;
// 要学会抽象出规律来,看看那些在循环
char m[1010][1010];
int n = s.size() / ( 2 * numRows - 2 ) + 1;
memset(m, '0', sizeof(m));
// 记录总的
int k = 0;
// 记录列数
int j = 0;
for(int i = 0; i < n && k < s.size(); i ++)
{
for(int p = 0; p < numRows && k < s.size(); p ++)
{
m[p][j] = s[k ++];
}
j ++;
for(int u = numRows - 2; u > 0 && k < s.size(); u --, j ++)
{
m[u][j] = s[k ++];
}
}
string res;
for(int i = 0; i < numRows; i ++)
{
for(int p = 0; p < j + 1; p ++)
{
if(m[i][p] != '0') res += m[i][p];
}
}
return res;
}
};
这是官方的代码, 设定一个x和y,然后根据不同的情况,让x和y进行不同的操作
class Solution {
public:
string convert(string s, int numRows) {
int n = s.length(), r = numRows;
if (r == 1 || r >= n) {
return s;
}
int t = r * 2 - 2;
int c = (n + t - 1) / t * (r - 1);
vector<string> mat(r, string(c, 0));
for (int i = 0, x = 0, y = 0; i < n; ++i) {
mat[x][y] = s[i];
if (i % t < r - 1) {
++x; // 向下移动
} else {
--x;
++y; // 向右上移动
}
}
string ans;
for (auto &row : mat) {
for (char ch : row) {
if (ch) {
ans += ch;
}
}
}
return ans;
}
};
边栏推荐
- MindSpore:【AIR模型导出】导出时提示源码中select_op参数类型转换失败
- Could you please talk about how the website is accessed?[Interview questions in the web field]
- MATLAB/Simulink快捷键
- 一道[CSCCTF 2019 Qual]FlaskLight的详解再遇SSTI
- After four years of outsourcing, the autumn recruits finally landed
- VRRP+MSTP配置详解【华为eNSP实验】
- leetcode每天5题-Day06
- 冰蝎逆向初探
- LeetCode中等题之设计循环队列
- 关于技术学习的6个观点
猜你喜欢

菲沃泰科创板上市:市值123亿 宗坚赵静艳夫妇身价76亿

VRRP+MSTP配置详解【华为eNSP实验】
![[Punctuality Atomic STM32 Serial] Chapter 1 Learning Method of the Book Excerpted from [Punctuality Atomic] MiniPro STM32H750 Development Guide_V1.1](/img/99/23e3dcb426769a415e33f7ff51bbb8.png)
[Punctuality Atomic STM32 Serial] Chapter 1 Learning Method of the Book Excerpted from [Punctuality Atomic] MiniPro STM32H750 Development Guide_V1.1

ps抠图怎么抠出来,自学ps软件photoshop2022,ps怎么抠出想要的部分-笔记记录
![[Punctuality Atom STM32 Serial] Chapter 4 STM32 First Experience Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1](/img/99/928e86f8a61a905a899dd5d3880def.png)
[Punctuality Atom STM32 Serial] Chapter 4 STM32 First Experience Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1

VRRP + MSTP configuration, huawei eNSP experiment 】 【

MindSpore:mirrorpad算子速度过慢的问题

思想茶叶蛋 (Jul 31,2022)| 元宇宙(Metaverse)下了一枚什么样的蛋
![Detailed explanation of telnet remote login aaa mode [Huawei eNSP]](/img/cf/aaf3a0b794b1076423fc5b90ecc9f0.png)
Detailed explanation of telnet remote login aaa mode [Huawei eNSP]
![cannot import name 'import_string' from 'werkzeug' [bug solution]](/img/ee/c91ec761eb637260d92980a2838a92.png)
cannot import name 'import_string' from 'werkzeug' [bug solution]
随机推荐
软件工程国考总结——判断题
leetcode每天5题-Day06
菲沃泰科创板上市:市值123亿 宗坚赵静艳夫妇身价76亿
双指针方法
MindSpore:图算融合报错
四大网络攻击常见手段及防护
Win11不识别蓝牙适配器的解决方法
【C补充】指向指针或函数的指针
三层交换机配置MSTP协议详解【华为eNSP实验】
leetcode经典例题——49.字母异位词分组
GBsae 8 c database using an error, how to do?
函数防抖与函数节流
Get the number of cpu cores
Fiddler(二)-手机抓包502错误解决方法
It is found that several WRH tables are locked, what should I do?
LeetCode581+621+207
【COS 加码福利】COS 用户实践有奖征文,等你来投稿!
区分惯性环节与延迟环节
我和 TiDB 的故事 | 缘份在,那就终是能相遇的
MATLAB/Simulink快捷键