6.ZigZag Conversion(Graph, traverse)
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 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 = ; //pointer to s
int pRow = ; //pointer to indicate row number
while(){
pRow = ;
while(pRow < numRows && pChar != s.length()){ //traverse first colomn
sArray[pRow++] += s[pChar++];
}
if(pChar == s.length()) return getReturnStr(sArray,numRows);
pRow = numRows-;
while(pRow > && 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 = ; i < numRows; i++){
ret += sArray[i];
}
return ret;
}
};
6.ZigZag Conversion(Graph, traverse)的更多相关文章
- 【leetcode❤python】 6. ZigZag Conversion
#-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object): def convert(self, s, numRow ...
- 64. ZigZag Conversion
ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...
- No.006 ZigZag Conversion
6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...
- leetcode第六题 ZigZag Conversion (java)
ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...
- leetcode题解 6.ZigZag Conversion
6.ZigZag Conversion 题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a gi ...
- 6.[leetcode] ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 字符串按照Z旋转90度然后上下翻转的字形按行输出字符串--ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- LeetCode--No.006 ZigZag Conversion
6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...
- leetcode-algorithms-6 ZigZag Conversion
leetcode-algorithms-6 ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag ...
随机推荐
- [转]NuGet 包升级
Update-Package 在 NuGet 的命令控制台执行这个就会升级所有 NuGet 包,不带参数. 使用 VS2015 时,插件 Web Extension Pack 2015 和 Web.E ...
- C#后台调用前台javascript的五种方法小结
第一种,OnClientClick (vs2003不支持这个方法) <asp:Button ID="Button1" runat="server" Tex ...
- Oracle内存详解之二 Library cache 库缓冲-转载
Library cache是Shared pool的一部分,它几乎是Oracle内存结构中最复杂的一部分,主要存放shared curosr(SQL)和PLSQL对象(function,procedu ...
- django的forms
base知识 参考博文:https://www.cnblogs.com/yuanchenqi/articles/9036474.html 用户表单是Web端的一项基本功能,大而全的Django框架中自 ...
- Spark SQL 编程
Spark SQL的依赖 Spark SQL的入口:SQLContext 官方网站参考 https://spark.apache.org/docs/1.6.2/sql-programming-guid ...
- Java反射 - 简单的给Bean赋值和取值
由于项目的实际需要,所以利用java反射原理写了一个简单给bean赋值和取值通用的类,在此记录下方便自己日后用到,也为需要的兄弟提供个参考例子. 工具类BeanRefUtil: package c ...
- mysql 分片
MySQL Fabric(分片) 是一个用于管理 MySQL 服务器群的可扩展框架.该框架实现了两个特性 — 高可用性 (HA ) 以及使用数据分片的横向扩展.这两个特性既可以单独使用,也可以结合使 ...
- js中的数组操作
<!DOCTYPE HTML> <html > <head> <meta http-equiv="Content-Type" conten ...
- 文件的编辑命令-echo/cat
touch test.yaml echo "line1 line2" >> test.yaml cat test.yaml line1 line2 # 创建test.y ...
- centos7.3安装zend guard loader3.3 for php5.6
1 下载zend guard loader 到这里选择自己的系统版本 我选择的64位 for php5.6.3 linux http://www.zend.com/en/products/load ...