《leetCode》:ZigZag Conversion
来源:互联网 发布:没有好奇心 知乎 编辑:程序博客网 时间:2024/06/08 03:37
题目描述如下
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 NA P L S I I GY I RAnd 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);convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
关于ZigZag的理解,见下图
题目意思就是:将字符串按照ZigZag格式进行摆放,然后按行组成的字符串输出。
实现代码如下:
/*思路如下:1、第i排从i开始2、 含numRows的每列相邻之间的元素的间隔为step=2*(numRows-1) 3、斜线上面的元素间隔为:step=j+step-2*i;其中j为s中的下标,i为行下标(从零开始) */ #include<stdio.h>#include<stdlib.h>#include<string.h> char* convert(char* s, int numRows) { if(s==NULL||numRows<1){ return NULL; } int len=strlen(s); if(numRows==1||numRows>=len){//当行数为1且numRows>=len时,直接输出即可 return s; } char *str=(char *)malloc(len*sizeof(char));//用来保存结果。 if(str==NULL){ exit(EXIT_FAILURE); } // int index=0; for(int i=0;i<numRows;i++){//行数 int step=2*(numRows-1); for(int j=i;j<len;j+=step){ str[index]=s[j]; index++; if(i==0||i==numRows-1){ continue; } //除第一行和最后一行之外,需要在两个元素之间加上 “斜线上”元素 int slashIndex=j+step-2*i; if(slashIndex<len){ str[index]=s[slashIndex]; index++; } } } return str; }int main(void){ char s[1000]; int k; while(gets(s)){ puts(s); scanf("%d",&k); char *result=convert(s,k); puts(result); }}
在自己电脑上面的编译环境发现没有问题。但是
按照如上代码提交:居然不能AC,原因是:除了输出符合要求的字符串之外,字符串后面不知道还输出了些什么。
经过查找原因发下是这样的:在C中,字符串是以“\n”结束的,而上面的代码的返回值不是以“\n”结束的,因此,需要在返回前的字符串最后加上”\n”即可。
具体需要改变的代码有:
char *str=(char *)malloc((len+1)*sizeof(char));//用来保存结果。...str[len]='\0';//在返回前的字符串最后加上"\n"return str;
完整代码如下:
char* convert(char* s, int numRows) { if(s==NULL||numRows<1){ return NULL; } int len=strlen(s); if(numRows==1||numRows>=len){//当行数为1且numRows>=len时,直接输出即可 return s; } char *str=(char *)malloc((len+1)*sizeof(char));//用来保存结果。 if(str==NULL){ exit(EXIT_FAILURE); } // int index=0; for(int i=0;i<numRows;i++){//行数 int step=2*(numRows-1); for(int j=i;j<len;j+=step){ str[index]=s[j]; index++; if(i==0||i==numRows-1){ continue; } //除第一行和最后一行之外,需要在两个元素之间加上 “斜线上”元素 int slashIndex=j+step-2*i; if(slashIndex<len){ str[index]=s[slashIndex]; index++; } } } str[len]='\0'; return str; }
小结
这道题花费了不少时间,首先是不能提取出规律,然后就是遇到了一些bug。在leetCode上面这道题的难度为:esay。哎,太打击我了。
0 0
- LeetCode: ZigZag Conversion
- LeetCode : ZigZag Conversion
- leetcode 13:zigzag conversion
- [LeetCode] Zigzag Conversion
- LeetCode 6 - ZigZag Conversion
- Leetcode : ZigZag Conversion
- 【leetcode】Zigzag Conversion
- [LeetCode]ZigZag Conversion
- leetcode ZigZag Conversion
- LeetCode-ZigZag Conversion
- LeetCode ZigZag Conversion
- LeetCode 6: ZigZag Conversion
- leetcode-006:ZigZag Conversion
- Leetcode: ZigZag Conversion
- [LeetCode]ZigZag Conversion
- [LeetCode] ZigZag Conversion
- [LeetCode]ZigZag Conversion
- leetcode: ZigZag Conversion
- Science上聚类算法论文——Clustering by fast search and find of density peaks翻译稿
- Win7 图标异常
- 验证码的实现
- Java大数类用法
- 下拉刷新及相关框架
- 《leetCode》:ZigZag Conversion
- Cocos适配带虚拟导航栏手机(HUAWEI 荣耀6 Plus)
- hdu 4280 Island Transport 最大流sap
- PHP面向对象1.2对象类型在内存中的分配
- 设计模式之建造者模式
- matlab使用Matlab进行特征选择
- kvm随笔系列四:AMD SVM
- sql--索引创建
- 一些有用的网站