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 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);
class Solution {
public:
string convert(string s, int numRows) {
}
};
P A H N ………………………………………… col[0]=4
A P L S I I G ………………………………………… col[1]=7
Y I R ………………………………………… col[2]=3
P A H N
A P L S I I G
Y I R
string a(10); //no exist such initialization but vector a(10) is okay
string a(10,’\0’) //but you can do like this
string convert(string s, int numRows)
{
//boundary conditions: (1) s.size()==0; (2) numRows == 1
//what about other impossible instances eg:numRows == 0
if(s.size() == 0)
{
return string();
}
else if(numRows == 1)
{
return string(s.begin(), s.end());
}
//initialize res, but we cannot do like this: string res(sz);
int sz = s.size();
string res(sz, '\0');
//col[] is to store every row's length
vector col(numRows, 0);
//compute col[0]
col[0] = (sz - 1) / ((numRows << 1) - 2) + 1;
//compute col[ 1, 2, ..., numRows-2 ]
for(int i = 1; i < numRows - 1; ++i)
{
if(sz - ((numRows - 1)*((col[0] - 1) << 1) + 1 + i) < 0)
{
col[i] = (col[0] - 1) << 1;
}
else if(sz - ((numRows - 1)*((col[0] - 1) << 1) +
(numRows << 1) - i - 1) < 0)
{
col[i] = (col[0] << 1) - 1;
}
else
{
col[i] = col[0] << 1;
}
}
//compute col[numRows-1]
if(sz - ((numRows - 1)*((col[0] - 1) << 1) + numRows) < 0)
{
col[numRows - 1] = col[0] - 1;
}
else
{
col[numRows - 1] = col[0];
}
//to get the res
for(int index = 0, i = 0; i < numRows&&index != sz; ++i)
{
if(i == 0 || i == numRows - 1)
{
for(int j = 0; j != col[i]; ++j)
{
res[index++] = s[(((numRows - 1)*j) << 1) + i];
}
}
else
{
for(int j = 0; j != col[i]; ++j)
{
if(j % 2 == 0)
{
res[index++] = s[(j >> 1)*((numRows - 1) << 1) + i];
}
else
{
res[index++] =
s[(j >> 1)*((numRows - 1) << 1) + (numRows << 1) - i - 2];
}
}
}
}
return res;
}
string convert(string s, int nRows)
{
if(nRows <= 1)
return s;
const int len = (int)s.length();
string *str = new string[nRows];
int temp = (len - 1) / ((nRows << 1) - 2) + 1;
int limits = temp << 1;
for(int i = 0; i < nRows; ++i)
{
str[i].reserve(limits);
}
int row = 0, step = 1;
for(int i = 0; i < len; ++i)
{
str[row].push_back(s[i]);
if(row == 0)
step = 1;
else if(row == nRows - 1)
step = -1;
row += step;
}
s.clear();
for(int j = 0; j < nRows; ++j)
{
s.append(str[j]);
}
delete[] str;
return s;
}
string convert(string s, int nRows)
{
if(nRows <= 1) return s;
string result = "";
//the size of a cycle(period)
int cycle = 2 * nRows - 2;
for(int i = 0; i < nRows; ++i)
{
for(int j = i; j < s.length(); j = j + cycle)
{
result = result + s[j];
//j-i 回跳到之前尖点[行号为0], +cycle 则是跳到下一个尖点
//-i 则是回跳到前面的第i行 思路很清晰
int secondJ = (j - i) + cycle - i;
if(i != 0 && i != nRows - 1 && secondJ < s.length())
result = result + s[secondJ];
}
}
return result;
}
size(),length() //等价,返回字符串大小;
capacity() //返回分配的内存空间的大小。这个不是指分配给对象string s的大小,而是对象里面有个指针,也就是s.c_str()所指向的对象所获得内存空间
max_size() //值就是npos-1 为什么不是npos?可能是为了存放\0吧。 可能值4294967294,不同编译器值可能不一样 这个无需深究
resize()
void resize (size_type n); //n小于目前的size,则截断;如果大于,则使用\0进行初始化多出来的元素
void resize (size_type n, charT c); //n小于目前的size,则截断;如果大于,则使用c进行初始化多出来的元素
#include
#include
using namespace std;
int main()
{
std::string str(200,'\0');
std::cout << "size1: " << str.size() << "\n";
std::cout << "length1: " << str.length() << "\n";
std::cout << "capacity1: " << str.capacity() << "\n";
std::cout << "max_size1: " << str.max_size() << "\n";
str.resize(3);
std::cout << "size2: " << str.size() << "\n";
std::cout << "capacity2: " << str.capacity() << "\n";
std::cout << std::string::npos << std::endl;
return 0;
}
string a{"I Love You"};
a.reserve(); //no effect
a.reserve(0); //no effect
a.reserve(100); //size no change; capacity rise to more than 100 and nearest to 100 就是大于100也是最靠近100的capacity 我的VS结果是111
bool empty() const; //看的是size,而不是capacity。由于是const函数,所以不改变size跟capacity.
void clear(); //size to 0; capacity no change