//6. ZigZag Conversion --String类型
来源:互联网 发布:淘宝窗帘拍摄技巧 编辑:程序博客网 时间:2024/06/07 22:57
leetcode 题目
The string "PAYPALISHIRING"
is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H NA P L S I I GY I RAnd then read line by line:
"PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3)
should return "PAHNAPLSIIGYIR"
.这个刚开始我还懵b--什么是zig形状啊。。。。
于是我搜索了一下,原来是这样:
偶,这样啊......看着就像求一个循环结构(就是一个形状不断重复)---然后求原数组元素在新的排列规则下的位置。那就求呗(高中数学题,有没有这感觉)
然后我先求竖的:j=step;//step代表他是一个周期内的第几个元素j代表该元素在新的n个String【indes】中的index;
斜的:j=2*numRows-step;
于是代码如下:
public class Solution{public String convert(String s,int numRows){if(numRows==1)return s;int k=numRows*2-2;String[] res=new String[numRows];for(int i=0;i<res.length;i++){res[i]="";}int step=0;int j=0;for(int i=0;i<s.length();i++){step++;if(step<=numRows){j=step;}else{j=2*numRows-step;}res[j-1]+=subString(i,i+1);if(ste==k)step=0;}String result="";for(String str:res){result+=str;}return result;}}
0 0
- //6. ZigZag Conversion --String类型
- string-6.ZigZag Conversion
- Leetcode - string - ZigZag Conversion
- **(leetcode_ string) ZigZag Conversion
- [leetcode][string] ZigZag Conversion
- String——zigzag-conversion
- ZigZag Conversion 矩阵对角类型
- LeetCode 6. ZigZag Conversion
- LeetCode --- 6. ZigZag Conversion
- LeetCode 6.ZigZag Conversion
- [Leetcode] 6. ZigZag Conversion
- 6.ZigZag Conversion
- 6. ZigZag Conversion
- 6. ZigZag Conversion
- 【leetcode】6. ZigZag Conversion
- 6.ZigZag Conversion
- 6. ZigZag Conversion
- 6.ZigZag Conversion
- android graphic(6)—surfaceflinger和MessageQueue
- 【设计模式】《Head First 设计模式》读书笔记——装饰者模式
- 10张图带你深入理解Docker容器和镜像
- cqround_7 游记
- 今日学习感悟
- //6. ZigZag Conversion --String类型
- AngularJS (一)
- ubutn16.04安装google浏览器
- php后台与客户端交互
- 【u033】地震逃生
- 【HPU 1011 QAQ的序列组合】
- 微信应用号,小程序开发教程第三弹
- 索引压缩
- 解决ItellJ IDEA中写JSP页面时out.print()报错问题