当前位置:网站首页>LeeCode -- 6. Z 字形变换
LeeCode -- 6. Z 字形变换
2022-07-07 21:51:00 【勤奋的凯尔森同学】
6. 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"
提示:
1 <= s.length <= 1000
s
由英文字母(小写和大写)、','
和'.'
组成1 <= numRows <= 1000
算法实现思路:
我们把字符串s当做一个数组,这个数组下标从零开始计数,一直到s的长度。按照Z转换的思想,把s的下标填到表格中。
当numRows=3的时候,排序如下:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CRRNabhx-1657178615583)(http://nas.hepcloud.top:6391/images/2022/07/07/image-20220707150657666.png)]
当numRows=6的时候,排序如下:
当numRows=n+1的时候,排序如下:
我们从第零行开始取字符,先取第0个,然后取第2n个,然后取4n个,一直取到字符串s可以取到的最大长度。接着去第一行,先取第1个,然后取第2n-1个,然后取第2n+1个,一直取到字符串s可以取到的最大长度。按照这个思路一直取到第n行。因为我们规定numRows=n+1,最后一行就是第n行。
我们设置变量i从0到n进行循环,标记为哪一行。设置变量j,j=0,2,4,6,8, … 之所以这样设置,是因为方便算某一行的下标位置。j * n + i为当前元素,那么(j + 2) * n - i就是紧接着它的后面一个元素的位置,假如有的话。限制j * n + i和(j + 2) * n - i都小于s的长度即可。
class Solution {
public String convert(String s, int numRows) {
int n = numRows - 1;
String res = "";
if(numRows == 1){
return s;
}
for(int i = 0; i < numRows; i++){
for(int j = 0; j * n + i < s.length(); j = j + 2){
//j * n + i为当前元素
if(j * n + i < s.length()){
res = res + String.valueOf(s.charAt(j * n + i));
}
//(j + 2) * n - i是j * n + i后面紧挨着的一个元素
if((j + 2) * n - i < s.length() && i != 0 && i != n){
res = res + String.valueOf(s.charAt((j + 2) * n - i));
}
}
}
return res;
}
}
边栏推荐
- Matlab-SEIR传染病模型预测
- Why does the market need low code?
- V20变频器手自动切换(就地远程切换)的具体方法示例
- CXF call reports an error. Could not find conduct initiator for address:
- Bit operation
- Cases of agile innovation and transformation of consumer goods enterprises
- One question per day - pat grade B 1002 questions
- Circumvention Technology: Registry
- Network security CSRF
- Wechat forum exchange applet system graduation design completion (4) opening report
猜你喜欢
Lecture 30 linear algebra Lecture 5 eigenvalues and eigenvectors
2021ICPC上海 H.Life is a Game Kruskal重构树
Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades-KDD2020
ArcGIS: two methods of attribute fusion of the same field of vector elements
I wish you all the best and the year of the tiger
USB(十五)2022-04-14
Matlab 信号处理【问答随笔·2】
Innovation today | five key elements for enterprises to promote innovation
Brush question 4
Inftnews | the wide application of NFT technology and its existing problems
随机推荐
Introduction to redis and jedis and redis things
Circumvention Technology: Registry
Inftnews | the wide application of NFT technology and its existing problems
Exploratory data analysis of heartbeat signal
Grid
php 使用阿里云存储
Adrnoid开发系列(二十五):使用AlertDialog创建各种类型的对话框
13、 System optimization
智慧社區和智慧城市之間有什麼异同
Locate to the bottom [easy to understand]
Lecture 30 linear algebra Lecture 5 eigenvalues and eigenvectors
成年人只有一份主业是要付出代价的,被人事劝退后,我哭了一整晚
The 19th Zhejiang Provincial Collegiate Programming Contest VP记录+补题
UE4_UE5结合罗技手柄(F710)使用记录
2022 words for yourself
十三、系统优化
iNFTnews | Web5 vs Web3:未来是一个过程,而不是目的地
USB (十八)2022-04-17
CXF call reports an error. Could not find conduct initiator for address:
智慧社区和智慧城市之间有什么异同