leetcode-ZigZag Convertion

来源:互联网 发布:ie8 数组 indexof 编辑:程序博客网 时间:2024/06/05 05:07

该题有两种解题思路

1.推算源字符串的第i个字符在结果字符串中的位置

2.推算结果字符串的第i个字符在源字符串中的位置

本文只给出思路1的实现方法:

方法1(12ms):该方法是第一遍写出来的方法,有很多不合理的计算方法,方法二是对其的优化,这里把它记下来只是为了以后作为借鉴,读者可以不看该方法

char* convert(char* s, int numRows) {
    //求每段齿的长度(由几个字母构成)
int len_tooth = numRows>2 ? 2*numRows-2 : numRows;
//求总齿数
int len_srcStr = strlen(s);
int count_tooth;
bool LastToothIsComplete;
if(len_srcStr%len_tooth==0)
{
LastToothIsComplete = true;
count_tooth = len_srcStr/len_tooth;
}
else
{
LastToothIsComplete = false;
count_tooth = len_srcStr/len_tooth +1;
}
//记录每个齿位的起始位置
int*Section = (int*)malloc(len_tooth*sizeof(int*));
for(int i=0;i<numRows;i++)
{
if(LastToothIsComplete)
{
if(i==0)
{
Section[i] = 0;
}
else
{
if(i==1)
Section[i] = count_tooth;
else
Section[i] = Section[i-1] + count_tooth*2;
}
}
else
{
if(i==0)
{
Section[i] = 0;
}
else
{
if(i==1)
Section[i] = count_tooth;
else
{
Section[i] = Section[i-1]+(count_tooth-1)*2;
int odd = len_srcStr%len_tooth;
if(odd>=(i-1+1))
Section[i]++;
if(odd>=numRows+(numRows -(i-1+1)))
Section[i]++;
}
}
}
}
//将原字符串中的字符放入相应齿位
char*rs = (char*)malloc((len_srcStr+1)*sizeof(char));
rs[len_srcStr] = '\0';
int currentTooth = 0;
for(int i=0;i<len_srcStr;i++)
{
int sectionId = i%len_tooth;
int pos_insert;
if(sectionId<numRows)
{
if((sectionId==0)||(sectionId == numRows-1))
{
pos_insert = Section[sectionId]+currentTooth;
}
else
{
pos_insert = Section[sectionId]+currentTooth*2;
}
}
else
{
pos_insert = Section[(numRows-1-1)-(sectionId-numRows)]+currentTooth*2+1;
}
if(sectionId==len_tooth-1) currentTooth++;
rs[pos_insert] = s[i];
}

return rs;
}

方法2(8ms):

该方法主要优化的部分是求每行的起始位置这部分:方法1是

char* convert(char* s, int numRows) {
//求每段齿的长度(由几个字母构成)
int len_tooth = numRows > 2 ? 2 * numRows - 2 : numRows;
//求总齿数
int len_srcStr = strlen(s);
int *lasttooth = (int*)malloc(numRows*sizeof(int));
memset(lasttooth, 0, numRows*sizeof(int));
int last = len_srcStr % len_tooth;
for (int i = 0; i < last; i++)
{
if (i < numRows)
lasttooth[i]++;
else
{
lasttooth[numRows - 2 - (i - numRows)]++;
}
}
//记录结果阵中每行的起始位置
int*Section = (int*)malloc(numRows*sizeof(int*));
//int toothnum = len_srcStr%len_tooth != 0 ? len_srcStr / len_tooth + 1 : len_srcStr / len_tooth;
int toothnum = len_srcStr / len_tooth;
int toothnum_m2 = toothnum * 2;
Section[0] = 0;
Section[1] = toothnum + lasttooth[0];
for (int i = 2; i < numRows; i++)
{
Section[i] = Section[i-1]+ toothnum_m2 + lasttooth[i-1];
}


//将原字符串中的字符放入相应齿位
char*rs = (char*)malloc((len_srcStr + 1)*sizeof(char));
rs[len_srcStr] = '\0';
int currentTooth = 0;
int currenBase = 0;
while (1)
{
//if (currenBase+len_tooth-1 > len_srcStr-1)优化前
if (currenBase + len_tooth > len_srcStr)
break;
for (int i = 0; i < numRows; i++)
{
int rsPos;
if ((i == 0) || (i == numRows - 1))
{
rs[Section[i] + currentTooth ] = s[currenBase + i];
}
else
{
rs[Section[i] + (currentTooth)* 2] = s[currenBase + i];
rs[Section[i] + (currentTooth)* 2 + 1] = s[currenBase + numRows + (numRows - 2 - i)];
}
}
currentTooth++;
currenBase += len_tooth;
}
//int last = len_srcStr%len_tooth;
for (int i = 0; i < last; i++)
{
if ((i == 0) || (i == numRows - 1))
{
rs[Section[i] + currentTooth] = s[currenBase + i];
}
else
{
if (i < numRows)
rs[Section[i] + (currentTooth)* 2] = s[currenBase + i];
else
rs[Section[1 + (numRows - 2 - (i - numRows + 1))] + (currentTooth)* 2 + 1] = s[currenBase + i];
}
}


return rs;
}

0 0
原创粉丝点击