Leetcode_6_ZigZagConvetsion

和初学C语言时,打印杨辉三角、三角形的“*”以及99乘法表类似,找到规律,判断好周期即可

不同的是,本题要考虑全面,注意一些特殊情况

char* convert(char* s, int numRows) {
	if(numRows==1)
		return s;
    char *ans=0,*ptr;
	int n=strlen(s),T=2*(numRows-1),rcount,i;
	ans=(char*)malloc(n+1);
	ptr=ans;
	for(rcount=0;rcount<numRows;rcount++)
	{
		if(rcount>=n)
			break;
		if(rcount==0 || rcount==numRows-1)
		{
			for(i=rcount;i<n;i=i+T)
			{
				*ptr=s[i];
				ptr++;
			}
		}
		else
		{
			for(i=rcount;i<n;i=i+T)
			{
				*ptr=s[i];
				ptr++;
				if(i+T-2*rcount<n)
				{
					*ptr=s[i+T-2*rcount];
					ptr++;
				}
			}
		}
	}
	*ptr=0;
	return ans;
}

最后纪念一下这个100%

Leetcode_6_ZigZagConvetsionLeetcode_6_ZigZagConvetsion