当前位置:网站首页>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;
}
};
边栏推荐
- After four years of outsourcing, the autumn recruits finally landed
- leetcode经典例题——49.字母异位词分组
- Apache APISIX 2.15 版本发布,为插件增加更多灵活性
- 【COS 加码福利】COS 用户实践有奖征文,等你来投稿!
- 云函数实现网站自动化签到配置详解【Web函数/Nodejs/cookie】
- 请问同一个oracle cdc表,如果flink job重新提交,是会全量读取一遍源数据还是增量呢?
- Post-94 Byte P7 posted the salary slip: It's really good to make up for this...
- 2022 Cloud Native Computing代表厂商 | 灵雀云第三次入选Gartner中国ICT技术成熟度曲线报告
- 双指针方法
- 命里有时终须有--记与TiDB的一次次擦肩而过
猜你喜欢

Since his 97, I roll but he...
![Detailed explanation of MSTP protocol configuration on Layer 3 switches [Huawei eNSP experiment]](/img/97/6c3662ef36b02bc42eec95abaa6bc5.png)
Detailed explanation of MSTP protocol configuration on Layer 3 switches [Huawei eNSP experiment]

leetcode每天5题-Day06

使用ClickHouse分析COS的清单和访问日志

LeetCode中等题之旋转图像

telnet远程登录aaa模式详解【华为eNSP】

三层交换机配置MSTP协议详解【华为eNSP实验】

字符串相关题目

外包干了四年,秋招终于上岸了

Grafana9.0发布,Prometheus和Loki查询生成器、全新导航、热图面板等新功能!
随机推荐
leetcode二叉树系列(一)
MATLAB绘图总结
今日睡眠质量记录71分
ps抠图怎么抠出来,自学ps软件photoshop2022,ps怎么抠出想要的部分-笔记记录
LVGL的多语言转换工具--字体设置的好助手
双指针方法
云函数实现网站自动化签到配置详解【Web函数/Nodejs/cookie】
学习在php中分析switch与ifelse的执行效率
How to restore the Youxuan database with only data files
他97年的,我既然卷不过他...
LeetCode中等题之旋转图像
Fiddler(一)安装
TiDB升级与案例分享(TiDB v4.0.1 → v5.4.1)
cannot import name ‘import_string‘ from ‘werkzeug‘【bug解决】
华为od项目
LeetCode581+621+207
有了这篇 Kubernetes 的介绍,它的原理秒懂!
NAT/NAPT地址转换(内外网通信)技术详解【华为eNSP】
【正点原子STM32连载】第四章 STM32初体验 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1
ps如何换背景颜色,自学ps软件photoshop2022,3种不同的方式笔记记录