当前位置:网站首页>LeeCode -- 6. Zigzag transformation
LeeCode -- 6. Zigzag transformation
2022-07-07 23:18:00 【Diligent classmate Kelsen】
6. Z Font conversion
Will a given string s
According to the given number of rows numRows
, From top to bottom 、 Left to right Z
Font arrangement .
For example, the input string is "PAYPALISHIRING"
The number of rows is 3
when , Arranged as follows :
P A H N
A P L S I I G
Y I R
after , Your output needs to be read line by line from left to right , Generate a new string , such as :"PAHNAPLSIIGYIR"
.
Please implement this function to transform a string into a specified number of lines :
string convert(string s, int numRows);
Example 1:
Input :s = "PAYPALISHIRING", numRows = 3
Output :"PAHNAPLSIIGYIR"
Example 2:
Input :s = "PAYPALISHIRING", numRows = 4
Output :"PINALSIGYAHRPI"
explain :
P I N
A L S I G
Y A H R
P I
Example 3:
Input :s = "A", numRows = 1
Output :"A"
Tips :
1 <= s.length <= 1000
s
By the English letters ( Lowercase and upper case )、','
and'.'
form1 <= numRows <= 1000
Algorithm implementation ideas :
We put the string s As an array , This array subscript counts from zero , Until s The length of . according to Z Transformational thinking , hold s Fill in the table with the subscript of .
When numRows=3 When , The order is as follows :
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-CRRNabhx-1657178615583)(http://nas.hepcloud.top:6391/images/2022/07/07/image-20220707150657666.png)]
When numRows=6 When , The order is as follows :
When numRows=n+1 When , The order is as follows :
Let's start with line zero and take characters , Take the first 0 individual , Then take the 2n individual , And then take 4n individual , Get the string all the way s The maximum length that can be taken . Then go to the first line , Take the first 1 individual , Then take the 2n-1 individual , Then take the 2n+1 individual , Get the string all the way s The maximum length that can be taken . Follow this idea until n That's ok . Because we stipulate that numRows=n+1, The last line is n That's ok .
Let's set the variable i from 0 To n Cycle through , Which line is marked . Set a variable j,j=0,2,4,6,8, … The reason for this setting is , Because it is convenient to calculate the subscript position of a line .j * n + i For the current element , that (j + 2) * n - i Is the position of the next element immediately after it , If there is one . Limit j * n + i and (j + 2) * n - i All less than s It's just as long as you want .
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 For the current element
if(j * n + i < s.length()){
res = res + String.valueOf(s.charAt(j * n + i));
}
//(j + 2) * n - i yes j * n + i The next element
if((j + 2) * n - i < s.length() && i != 0 && i != n){
res = res + String.valueOf(s.charAt((j + 2) * n - i));
}
}
}
return res;
}
}
边栏推荐
- Solution: prompt "unsupported video format" when inserting avi format video into the message
- 30讲 线性代数 第五讲 特征值与特征向量
- 网络安全-CSRF
- Innovation today | five key elements for enterprises to promote innovation
- CXF call reports an error. Could not find conduct initiator for address:
- 七月第一周
- The 19th Zhejiang Provincial Collegiate Programming Contest VP记录+补题
- Wechat forum exchange applet system graduation design completion (4) opening report
- leetcode-520. 检测大写字母-js
- Txt file virus
猜你喜欢
UE4_UE5结合罗技手柄(F710)使用记录
Digital collections accelerated out of the circle, and marsnft helped diversify the culture and tourism economy!
十四、数据库的导出和导入的两种方法
海内外技术人们“看”音视频技术的未来
LDO稳压芯片-内部框图及选型参数
Wechat forum exchange applet system graduation design (2) applet function
Wechat forum exchange applet system graduation design completion (7) Interim inspection report
Wechat forum exchange applet system graduation design completion (1) development outline
U盘拷贝东西时,报错卷错误,请运行chkdsk
ArcGIS: two methods of attribute fusion of the same field of vector elements
随机推荐
ROS2专题(03):ROS1和ROS2的区别【02】
云原生数据仓库AnalyticDB MySQL版用户手册
PMP项目管理考试过关口诀-1
Two kinds of curves in embedded audio development
JMeter interface automated test read case, execute and write back result
Digital collections accelerated out of the circle, and marsnft helped diversify the culture and tourism economy!
十三、系统优化
智慧社區和智慧城市之間有什麼异同
微信论坛交流小程序系统毕业设计毕设(8)毕业设计论文模板
嵌入式音频开发中的两种曲线
OC variable parameter transfer
解决:信息中插入avi格式的视频时,提示“unsupported video format”
Wechat forum exchange applet system graduation design completion (6) opening defense ppt
Network security - phishing
U盘拷贝东西时,报错卷错误,请运行chkdsk
Wechat forum exchange applet system graduation design completion (4) opening report
UE4_UE5结合罗技手柄(F710)使用记录
Add data analysis tools in Excel
经纬度PLT文件格式说明
Network security -burpsuit