LeetCode ZigZag Conversion
2015-11-06 10:57
169 查看
The string
this: (you may want to display this pattern in a fixed font for better legibility)
And then read line by line:
Write the code that will take a string and make this conversion given a number of rows:
return
用简单的模拟来做,下面附上我的代码:
同学的代码:
其思路是每个循环赋值好一个竖排加上一个对角线:
"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 N A P L S I I G Y I R
And 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".
用简单的模拟来做,下面附上我的代码:
<pre name="code" class="cpp">class Solution { public: char **arr; string convert(string s, int numRows) { if (numRows==1||s.length()==0) { return s; } //计算行数列数 int Y; if ((numRows*2-2)!=0) { Y=(int)((numRows*s.length())/(numRows*2-2))+1; }else{ Y=1; } int X=numRows; arr= new char *[X]; for (int i=0; i<X; i++) { arr[i]=new char[Y]; } //初始化数组 for (int i=0; i<X; i++) { for (int j=0; j<Y; j++) { arr[i][j]=0; } } string ans; int vertical=1; int i=0; int j=0;//数组下标 for (int k =0; k<s.length(); k++) { arr[i][j]=s[k]; //逐个字符遍历 if (vertical&&i!=numRows-1) {//如果是竖直的并且还没到num个 i++;//下一次摆放的位置 continue; } if (vertical&&i==numRows-1) {//如果是竖着的已经num个了 vertical=0; i--; j++;//下一次摆放的位置 continue; } if (!vertical&&i!=0) {//如果是对角线,没到顶。 i--;j++;//下一次摆放的位置。 continue; } if (!vertical&&i==0) {//如果是对角线,到顶了。 i++;//下一次开始一个新的竖着的 vertical=1; } } ans=toString(s.length(),X, Y); return ans; } string toString(int size,int X,int Y){ char temparr[1000]; int count=0; memset(temparr, 0, sizeof(temparr)); for (int i=0; i<X; i++) { for (int j=0; j<Y; j++) { char c=arr[i][j]; if (c!=0) { temparr[count++]=c; size--; if (size==0) { break; } } } } return (string)temparr; } };
同学的代码:
其思路是每个循环赋值好一个竖排加上一个对角线:
string convert(string s, int numRows){ if(numRows == 1) return s; long len = s.size(); long n = (len/(2*numRows-2) +1)*numRows; char **array; array = new char*[numRows]; for(int i = 0; i < numRows; i++){ array[i] = new char ; } for(int i = 0; i < numRows; i++) for(int j = 0; j < n; j++) array[i][j] = ' '; int cur = 0, i = 0, j = 0; while(cur < s.size()){ for(; i < numRows && cur < s.size(); i++, cur++) array[i][j] = s[cur]; i -= 2; j++; for(; i >= 0 && cur < s.size(); i--, j++, cur++) array[i][j] = s[cur]; i += 2; } string s1; for(i = 0; i < numRows; i++) for(j = 0; j < n; j++) if(array[i][j] != ' ') s1 += array[i][j]; return s1; }
相关文章推荐
- iOS CoreAnimation动画系列教程
- 6个技巧让你成为专业程序员
- mysql group by 用法解析(详细)
- hibernate one-to-many,one-to-one,many-to-one怎么理解??
- LeetCode -- Longest Common Prefix
- 距离传感器的使用
- json_decode
- less命令
- 如何修正“must override a superclass method”错误。
- android 代码设置 color的selector
- Android布局属性大全详解
- IOS 项目配置--构建输出DIR
- Scala Web 框架——Lift(一)准备工作
- iOS开发获取本机手机号码
- bootsrap学习
- C++ Primer 学习笔记_10_数组
- 【数据结构】BST:二叉排序树算法
- 倍数提高工作效率的 Android Studio 奇技
- 灵活掌握asp.net中gridview控件的多种使用方法(下)
- Linux下磁盘分区挂载