6. ZigZag Conversion
2016-05-26 12:08
232 查看
The string
this: (you may want to display this pattern in a fixed font for better legibility)
And then read line by line:
Write the code that will take a string and make this conversion given a number of rows:
return
that's the zigzag pattern the question asked! Be careful with nR=1 && nR=2
use numRows space and easy to understand ,while my answer search the math solution,not easy to understand ,but is
interesting
string convert(string s, int numRows)
{
int length = s.size();
if(numRows <= 1 || length <= numRows)
return s;
string sZigzag;
for(int iLineIndex = 0; iLineIndex < numRows; iLineIndex++)
{
int iRowIndex = iLineIndex;
for(; iRowIndex < length; iRowIndex += 2 * (numRows - 1))
{
if(0 == iLineIndex || numRows - 1 == iLineIndex)
sZigzag = sZigzag + s[iRowIndex];
else
{
if(iRowIndex == iLineIndex)
sZigzag = sZigzag + s[iRowIndex];
else
{
sZigzag = sZigzag + s[iRowIndex - 2 * iLineIndex];
sZigzag = sZigzag + s[iRowIndex];
}
}
}
if((iLineIndex > 0) && (iLineIndex < numRows - 1) && (iRowIndex - 2 * iLineIndex < length))
sZigzag = sZigzag + s[iRowIndex - 2 * iLineIndex];
}
return sZigzag;
}
The problem statement itself is unclear for many. Especially for 2-row case. "ABCD", 2 --> "ACBD". The confusion most likely is from the character placement. I would like to extend it a little bit to make ZigZag easy understood.
The example can be written as follow:
P.......A........H.......N
..A..P....L..S....I...I....G
....Y.........I........R
Therefore, <ABCD, 2> can be arranged as:
A....C
...B....D
My simple accepted code:
class Solution {
public:
string convert(string s, int numRows) {
int len=s.size();
if(numRows<=1||len<=numRows) return s;
vector<string> vs(numRows);
string ss;
int step,row=0;
for(int i=0;i<len;i++)
{
vs[row].push_back(s[i]);
if(row==0) step=1;
else if(row==numRows-1) step=-1;
row+=step;
}
for(int j=0;j<numRows;j++)
ss.append(vs[j]);
return ss;
}
};
"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 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 and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3)should
return
"PAHNAPLSIIGYIR".
/*n=numRows Δ=2n-2 1 2n-1 4n-3 Δ= 2 2n-2 2n 4n-4 4n-2 Δ= 3 2n-3 2n+1 4n-5 . Δ= . . . . . Δ= . n+2 . 3n . Δ= n-1 n+1 3n-3 3n-1 5n-5 Δ=2n-2 n 3n-2 5n-4 */
that's the zigzag pattern the question asked! Be careful with nR=1 && nR=2
class Solution { public: string convert(string s, int numRows) { string result=""; if(numRows==1) return s; int step1,step2; int len=s.size(); for(int i=0;i<numRows;++i){ step1=(numRows-i-1)*2; step2=(i)*2; int pos=i; if(pos<len) result+=s.at(pos); while(1){ pos+=step1; if(pos>=len) break; if(step1) result+=s.at(pos); pos+=step2; if(pos>=len) break; if(step2) result+=s.at(pos); } } return result; } };
use numRows space and easy to understand ,while my answer search the math solution,not easy to understand ,but is
interesting
string convert(string s, int numRows)
{
int length = s.size();
if(numRows <= 1 || length <= numRows)
return s;
string sZigzag;
for(int iLineIndex = 0; iLineIndex < numRows; iLineIndex++)
{
int iRowIndex = iLineIndex;
for(; iRowIndex < length; iRowIndex += 2 * (numRows - 1))
{
if(0 == iLineIndex || numRows - 1 == iLineIndex)
sZigzag = sZigzag + s[iRowIndex];
else
{
if(iRowIndex == iLineIndex)
sZigzag = sZigzag + s[iRowIndex];
else
{
sZigzag = sZigzag + s[iRowIndex - 2 * iLineIndex];
sZigzag = sZigzag + s[iRowIndex];
}
}
}
if((iLineIndex > 0) && (iLineIndex < numRows - 1) && (iRowIndex - 2 * iLineIndex < length))
sZigzag = sZigzag + s[iRowIndex - 2 * iLineIndex];
}
return sZigzag;
}
The problem statement itself is unclear for many. Especially for 2-row case. "ABCD", 2 --> "ACBD". The confusion most likely is from the character placement. I would like to extend it a little bit to make ZigZag easy understood.
The example can be written as follow:
P.......A........H.......N
..A..P....L..S....I...I....G
....Y.........I........R
Therefore, <ABCD, 2> can be arranged as:
A....C
...B....D
My simple accepted code:
class Solution {
public:
string convert(string s, int numRows) {
int len=s.size();
if(numRows<=1||len<=numRows) return s;
vector<string> vs(numRows);
string ss;
int step,row=0;
for(int i=0;i<len;i++)
{
vs[row].push_back(s[i]);
if(row==0) step=1;
else if(row==numRows-1) step=-1;
row+=step;
}
for(int j=0;j<numRows;j++)
ss.append(vs[j]);
return ss;
}
};
相关文章推荐
- VirtualBox P2V Physical to Virtual in VirtualBox
- 丢失Android系统库或者Conversion to Dalvik format failed with error 1错误的解决
- ZigZag Conversion
- [LeetCode] ZigZag Conversion
- LeetCode 6 ZigZag
- ZigZag Conversion
- How to Convert 10g Single-Instance database to 10g RAC using Manual Conversion procedure (文档 ID 7474
- ZigZag Conversion
- ZigZag Conversion解题报告
- ZigZag Conversion
- LeetCode 6 - ZigZag Conversion
- 【Leetcode】ZigZag Conversion
- LeetCode(60)-ZigZag Conversion
- [leetcode]ZigZag Conversion
- OCP-1Z0-051 第85题 显式转换和隐式转换
- [Leetcode 6] ZigZag问题的一种新思路
- Leetcode || ZigZag Conversion
- DSP 投放的基本流程和算法
- PowerShell查询AD域内长期没有登录的计算机对象
- Conversion to Dalvik format failed with error 1 解决问题