【leetcode c++】ZigZag Conversion
2015-06-05 00:24
239 查看
题目原文
The string "PAYPALISHIRING" iswritten in a zigzag pattern on a given number of rows like this: (you may wantto display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string andmake this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3)should return "PAHNAPLSIIGYIR".
很遗憾本人没有读懂题目,而是通过别人的博客了解的题目的要求的,上链接
http://blog.csdn.net/zhouworld16/article/details/14121477
另外这位博主是通过寻找下标规律来重组字符串的。
介绍一下所谓的zigzag(锯齿状)。
首先,把原始字符串这么摆:
没错,是这么摆。
接下来画n条直线与之相交:我们这里画4条
相交之后就会产生交点:输入字符串的字符就排列在这些交点上啦。
这些交点重组之后的n个字符串接起来就是题目要求的输出了
也就是resString =string_1 + string_2 + string_3 + string_4;
/////////////////////////////////////////////////////////////////////////////////////////////////
当n=1的时候,输入等于输出。
当n=2的时候,输出就是下标为偶数和下标为奇数的两个重组的字符串接起来。
依据上述的思路的解法就是,创建一个存有n个string的数组,用一个迭代器获取数组中的字符串。
获取第1个交点,把它加到string_1中,迭代器后移,指向string_2。
获取第2个交点,把它加到string_2中,迭代器后移,指向string_3。
……
迭代器到达边缘时转向,直到扫描完所有交点(输入字符串)。
Leetcode的Accepted Solutions Runtime Distribution(截于5月上旬,灰色柱子是所处)
这里因为使用数组和迭代器,所以相比于利用规律来重组字符串的速度要慢一些。关于利用规律的解法请参照开头提供的链接。
源码:(VS2013)如果需要提交leetcode只需要把convertZigZag函数中的代码复制过去即可。
#include <iostream>
#include <map>
#include <vector>
#include <string>
using namespace std;
string convertZigZag(string s, int numRows);
int main()
{
string s1 = "PAYPALISHIRING";
cout << convertZigZag(s1, 3);
return 0;
}
string convertZigZag(string s, int numRows) {
if (1 == numRows)
{
return s;
}
if (2 == numRows)
{
string::iterator iter;
iter = s.begin();
string s1;
string s2;
while (iter != s.end())
{
s1 += *iter;
iter++;
if (iter != s.end())
{
s2 += *iter;
iter++;
}
}
return s1 + s2;
}
else
{
vector<string> sRes;
vector<string>::iterator iterSRes;
string::iterator iterS;
for (int i = 0; i < numRows; i++)
{
string s2;
sRes.push_back(s2);
}
int max = s.size();
bool down = false;
int count = 0;
iterSRes = sRes.begin();
iterS = s.begin();
while (count < max)
{
if (0 == count % (numRows - 1))
{
if (down) down = false;
else down = true;
}
*iterSRes += *iterS;
if (down)
{
iterSRes++;
}
else
{
iterSRes--;
}
iterS++;
count++;
}
string res;
iterSRes = sRes.begin();
while (iterSRes != sRes.end())
{
res += *iterSRes;
iterSRes++;
}
return res;
}
}
The string "PAYPALISHIRING" iswritten in a zigzag pattern on a given number of rows like this: (you may wantto display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string andmake this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3)should return "PAHNAPLSIIGYIR".
很遗憾本人没有读懂题目,而是通过别人的博客了解的题目的要求的,上链接
http://blog.csdn.net/zhouworld16/article/details/14121477
另外这位博主是通过寻找下标规律来重组字符串的。
介绍一下所谓的zigzag(锯齿状)。
首先,把原始字符串这么摆:
没错,是这么摆。
接下来画n条直线与之相交:我们这里画4条
相交之后就会产生交点:输入字符串的字符就排列在这些交点上啦。
这些交点重组之后的n个字符串接起来就是题目要求的输出了
也就是resString =string_1 + string_2 + string_3 + string_4;
/////////////////////////////////////////////////////////////////////////////////////////////////
当n=1的时候,输入等于输出。
当n=2的时候,输出就是下标为偶数和下标为奇数的两个重组的字符串接起来。
依据上述的思路的解法就是,创建一个存有n个string的数组,用一个迭代器获取数组中的字符串。
获取第1个交点,把它加到string_1中,迭代器后移,指向string_2。
获取第2个交点,把它加到string_2中,迭代器后移,指向string_3。
……
迭代器到达边缘时转向,直到扫描完所有交点(输入字符串)。
Leetcode的Accepted Solutions Runtime Distribution(截于5月上旬,灰色柱子是所处)
这里因为使用数组和迭代器,所以相比于利用规律来重组字符串的速度要慢一些。关于利用规律的解法请参照开头提供的链接。
源码:(VS2013)如果需要提交leetcode只需要把convertZigZag函数中的代码复制过去即可。
#include <iostream>
#include <map>
#include <vector>
#include <string>
using namespace std;
string convertZigZag(string s, int numRows);
int main()
{
string s1 = "PAYPALISHIRING";
cout << convertZigZag(s1, 3);
return 0;
}
string convertZigZag(string s, int numRows) {
if (1 == numRows)
{
return s;
}
if (2 == numRows)
{
string::iterator iter;
iter = s.begin();
string s1;
string s2;
while (iter != s.end())
{
s1 += *iter;
iter++;
if (iter != s.end())
{
s2 += *iter;
iter++;
}
}
return s1 + s2;
}
else
{
vector<string> sRes;
vector<string>::iterator iterSRes;
string::iterator iterS;
for (int i = 0; i < numRows; i++)
{
string s2;
sRes.push_back(s2);
}
int max = s.size();
bool down = false;
int count = 0;
iterSRes = sRes.begin();
iterS = s.begin();
while (count < max)
{
if (0 == count % (numRows - 1))
{
if (down) down = false;
else down = true;
}
*iterSRes += *iterS;
if (down)
{
iterSRes++;
}
else
{
iterSRes--;
}
iterS++;
count++;
}
string res;
iterSRes = sRes.begin();
while (iterSRes != sRes.end())
{
res += *iterSRes;
iterSRes++;
}
return res;
}
}
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解
- LeetCode [Day 5] Longest Palindromic Substring 题解
- LeetCode [Day 6] ZigZag Conversion 题解
- LeetCode 2: Add Two Numbers (JAVA)
- LeetCode 1: Two Sum (JAVA)