当前位置:网站首页>LeetCode中等题题分享(5)
LeetCode中等题题分享(5)
2022-07-01 23:54:00 【PigeonEssence】
Z字形变换
题目:
遇到这种题有横有竖的,我首先就想到的二维数组的解法:
说是Z字形,其实更像是一下一上的重复循环,所以我们思考的是每一个循环需要多少个字符:
假设rowNum=3,那么中间就是1个,总共需要4个
假设rowNum=4,那么中间就是2个,总共需要6个
假设rowNum=5,那么中间就是3个,总共需要8个
那么很明显,一下一上总共需要的就是2*rowNum-2个。
我们可以把这一次的上下看做一个循环,我们考虑一个循环在二维数组的空间占用
我们假设这个数组的长度是length:
int length = s.length();
numRows个横列:
int numOfRec = 2*numRows-2;
竖列的占用我们可以考虑是从numRows递减到0的一个forLoop,总的竖列我们主要考虑循环了多少次就可以了:
int numCols = (length+numOfRec-1)/numOfRec*(numRows-1);
然后我们需要做的就是遍历加入二维数组就行了:
//先往下填写numRows个字符,然后自左下到右上填写
public static String convert(String s, int numRows) {
int length = s.length();
if (numRows==1 || numRows>=length){
return s;
}
// 一循环所需要的字符数
int numOfRec = 2*numRows-2;
//列数
int numCols = (length+numOfRec-1)/numOfRec*(numRows-1);
//创建二维数组
char[][] cars = new char[numRows][numCols];
//遍历插入二维数组
for (int i = 0, x = 0, y = 0; i < length ; ++i) {
cars[x][y]=s.charAt(i);
if (i%numOfRec<numRows-1){
++x;
}else{
--x;
++y;
}
}
//遍历输出
StringBuilder res = new StringBuilder();
for (int j = 0; j < numRows; j++) {
for (int i = 0; i < numCols; i++) {
char c = cars[j][i];
if (c!='\u0000'){
res.append(c);
}
}
}
return res.toString();
}
这道题直观上来说就是二维数组+循环的使用问题,解题方法有一些暴力。需要考虑的难点就是二维数组的双重for循环会导致超时,在此程度上应该有所化简。
结果虽然过了但是不是很满意,以后有机会再优化吧
边栏推荐
- Record the accidental success and failure of uploading large files
- Notblank and notempty
- 关联性——组内相关系数
- 2022年最佳智能家居开源系统:Alexa、Home Assistant、HomeKit生态系统介绍
- notBlank 和 notEmpty
- 正则表达式收集
- Openwrt enable kV roaming
- .env.xxx 文件,加了常量,却undefined
- Is it safe to buy funds on Great Wall Securities?
- PostgreSQL source code (57) why is the performance gap so large in hot update?
猜你喜欢
随机推荐
使用VB.net将PNG图片转成icon类型图标文件
RPA教程01:EXCEL自动化从入门到实操
Use the htaccess file to prohibit the script execution permission in the directory
Reproduction process and problems of analog transformer (ICLR 2022 Spotlight)
华为HMS Core携手超图为三维GIS注入新动能
Linux CentOS7安装Oracle11g的超完美新手教程
2021 robocom world robot developer competition - preliminary competition of undergraduate group
Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
【QT】Qt 使用MSVC2017找不到编译器的解决办法
哈工大《信息内容安全》课程知识要点和难点
Anomaly-Transformer (ICLR 2022 Spotlight)复现过程及问题
安全协议重点
BlocProvider为什么感觉和Provider很相似?
Multi table operation - one to one, one to many and many to many
Windows 7 install MySQL error: 1067
. env. XXX file, with constant, but undefined
深度学习 | 三个概念:Epoch, Batch, Iteration
13 MySQL-约束
algolia 搜索需求,做的快自闭了...
.env.xxx 文件,加了常量,却undefined