#include<iostream>
#include<vector>
#include<string>
//要加上命名空间,否则会报错string
using namespace std;
//我也是个白痴,C++要把类声明什么的放在前面,我认为是因为顺序编译吧!不这么放会报错的!!
//以后我会把类声明放前面的
class Solution {
public:
string convert(string s, int numRows) {
//ctl+k+c注释掉,ctl+k+f去注释(visio studio)
//ctl+shift+c注释,tl+shift+x去注释(codeblocks)
if(s.length() <= 1 || numRows <= 0){
return s;
}
int len = s.length();
string res = "";
for(int i = 0; i < len && i < numRows ; i++){
res += s[i];
int index = i;
//index 小于 len
for(int k = 0;index < len ;k++){
if(i == 0 || i == numRows-1){
//对于第一行和最后一行的位置规律
index = index + 2*(numRows - 1);
}
else{
//判断奇偶列
//对中间行的规律
if( k & 0x1){
//奇数列
index = index + 2 * (numRows - i - 1);
}
else{
//偶列
index = index + 2 * i;
}
}
//对当前行测试,是否游标超过最值
if(index < len)
{
res = res + s[index];
}
}
}
return res;
}
};
int main(){
int numRows;
string str = "0123456789";
Solution m;
string a ;
//cin>>numRows;
numRows = 3;
//char* chr="paythemoney";
// getline(cin,str);
//Solution *aa = new Solution();
// a = m.convert(str,numRows);
cout<<m.convert(str,numRows)<<endl;
while(1);
return 0;
}
leetcode zigzag C++ 争取每日一题,我还是太天真了/(ㄒoㄒ)/~~
最新推荐文章于 2022-03-15 09:30:00 发布
