leetcode-------------ZigZag Conversion
来源:互联网 发布:php fpm mysql长连接 编辑:程序博客网 时间:2024/06/05 20:54
题目: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"
.看规律:
再来看另外一位大神的解释:
因为这是有规律的,可以用数学公式计算出下一个数组下标,这样节省了一定的空间和时间。 题目的输出是按行来拼接字符的,
因此我们可以通过逐行计算下标,拼接字符来达到目的。
通过画图观察,我们可以发现,
第0行和最后一行中,前一个下标的值和后一个下标的值相差 2 * nRows - 2 ,以第0行为例,前一个下标为0,
后一个下标为 0+2*4-2=6。
中间行中,前一个下标的值和后一个下标的值需要根据这个下标是该行中的奇数列还是偶数列来计算。以平时的习惯
来计算,因此,行和列的开始值都是0。
以第2行为例,第一个下标是2,后一个下标所处列为1,是奇数列,因此从这个下标到下一个下标相差的值是它们所处
的行i下面的所有行的点的个数,即2 * (nRows - 1 - i)。在这里,nRows-1是为了遵循0下标开始的原则。这样,我们可以
求得这个奇数列的下标为 2+2*(4-1-2)=4。同理,当我们要计算4之后的下标时,我们发现下一个所处的是偶数列2,从这个
下标到下一个下标相差的值其实是它们所处的行i上面的所有行的点的个数,即2 * i。因此,该列的下标计算为4+2*2=8。
最后上代码:
class Solution {public:string convert(string s, int nRows) {if (nRows == 1)return s;string temp;for (int i = 0; i<nRows; ++i){int j = i;bool flag = true;while (j<s.size()){temp.push_back(s[j]);if (i == 0 || i == nRows - 1)j += (nRows << 1) - 2;else{if (flag){j += ((nRows - i) << 1) - 2;flag = false;}else{j += (i << 1);flag = true;}}}}return temp;}};
另外直观的写法:
class Solution { public: string convert(string s, int nRows) { if (nRows==1) { return s; } string temp; int t_nlen=s.length(); int i,j; for (i=0;i<nRows;++i) { for (j=i;j<t_nlen;) { if (i==0||i==nRows-1) { temp.push_back(s[j]); j+=(nRows<<1)-2; } else { //int t_ntemp=((nRows-i)<<1)-2; temp.push_back(s[j]); j+=((nRows-i)<<1)-2; if (j<t_nlen) { temp.push_back(s[j]); j+=(i<<1); } /*else return temp;*/ } } } return temp; } };
某位大神的写的:
string convert(string s, int nRows) {string result;if (nRows == 1) return s;for (int i = 0; i < nRows; i++) {int j = i;bool flag = true;while (j < s.size()) {result.push_back(s[j]);if (i == 0 || i == nRows - 1) j += 2 * (nRows - 1);else {if (flag){j += 2 * (nRows - 1 - i);flag = false;}else {j += 2 * i;flag = true;}}}}return result;}
- LeetCode: ZigZag Conversion
- LeetCode : ZigZag Conversion
- leetcode 13:zigzag conversion
- [LeetCode] Zigzag Conversion
- LeetCode 6 - ZigZag Conversion
- Leetcode : ZigZag Conversion
- 【leetcode】Zigzag Conversion
- [LeetCode]ZigZag Conversion
- leetcode ZigZag Conversion
- LeetCode-ZigZag Conversion
- LeetCode ZigZag Conversion
- LeetCode 6: ZigZag Conversion
- leetcode-006:ZigZag Conversion
- Leetcode: ZigZag Conversion
- [LeetCode]ZigZag Conversion
- [LeetCode] ZigZag Conversion
- [LeetCode]ZigZag Conversion
- leetcode: ZigZag Conversion
- 【ArcGIS平台系列】Server,新一代Web GIS平台的重要支撑
- redis源码学习(客户端)
- JPEG——照片EXIF信息各种软件摘要
- poj1062
- vlc android的编译及截图,录制视频等功能
- leetcode-------------ZigZag Conversion
- iOS动画进阶(三)图层几何学
- Android支付接入(八):Amazon亚马逊支付
- libxml2.dylb 添加后 引用不了<libxml/tree.h>
- Shell Step by Step (1) —— Introduction
- Jmeter集合点
- Java类设计
- 使用nginx实现js跨域
- ,2015道奇杜兰戈评析