6.ZigZag Conversion(Graph, traverse)
2015-07-15 09:13
344 查看
The string
And then read line by line:
Write the code that will take a string and make this conversion given a number of rows:
思路:图的遍历,先纵向,再横向。
图用string数组存储。
如果nRows=4,注意短的那列是倒序。
P I N
A L S I G
Y A H R
P I
如果nRows=2,全部是正序。
P Y ...
A P ...
"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".
思路:图的遍历,先纵向,再横向。
图用string数组存储。
如果nRows=4,注意短的那列是倒序。
P I N
A L S I G
Y A H R
P I
如果nRows=2,全部是正序。
P Y ...
A P ...
class Solution { public: string convert(string s, int numRows) { string sArray[numRows]; int pChar = 0; //pointer to s int pRow = 0; //pointer to indicate row number while(1){ pRow = 0; while(pRow < numRows && pChar != s.length()){ //traverse first colomn sArray[pRow++] += s[pChar++]; } if(pChar == s.length()) return getReturnStr(sArray,numRows); pRow = numRows-2; while(pRow > 0 && pChar != s.length()){ sArray[pRow--] += s[pChar++]; } if(pChar == s.length()) return getReturnStr(sArray,numRows); } } string getReturnStr(string* sArray, int numRows){ string ret = ""; for(int i = 0; i < numRows; i++){ ret += sArray[i]; } return ret; } };
相关文章推荐
- Java集合之TreeMap源码分析
- ccBPM典型的树形表单和多表头表单的流程
- android https(SSL) 双向验证详解
- 架构师速成7.4-架构师为什么要带团队
- 表空间_临时表空间引起的错误:ora-01652 小例
- Entity Framework中编辑时错误ObjectStateManager 中已存在具有同一键的对象
- 架构师速成7.4-架构师为什么要带团队 分类: 架构师速成 2015-07-15 09:12 558人阅读 评论(2) 收藏
- (转)显示实现接口目的
- iOS 9 分屏多任务:画中画(PiP)快速入门(中文版)
- 将数据写入手机内存和sdcard中的文件
- mysql 中文乱码问题(mysql 5.1 , debian 6)
- NSIS updata升级问题
- 流程图符号及流程图的例子
- ios与javascript的交互,适合刚接触电商ios与html5混编的朋友,(ios自学笔记)
- js获取当前时间
- Elasticsearch的各种插件
- C++ 大规模程序设计 之 层次化
- 收到微软寄来的MVP包裹
- windows根据端口号查进程
- Boost.PropertyTree读取ini文件(Linux环境)