leetcode zigzag C++ 争取每日一题,我还是太天真了/(ㄒoㄒ)/~~

本文介绍了一个使用C++实现的字符串转换算法,该算法能够将输入的字符串按照行数进行特定规则的重新排列,并提供了完整的代码实现及测试示例。
#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值