6. ZigZag Conversion

题面

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 s, int numRows);

样例

Example 1:

Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"

Example 2:

Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation: P I N
A L S I G
Y A H R
P I
 //思路1:
//完全按照样例来模拟过程
//时间:O(n2)
//空间:O(n2)
1 class Solution {
public:
string convert(string s, int numRows) {
int len = s.length();
if(len < )
return s;
char ans[len][len];
       //记忆化数组初始化为'#'
for (int i = ; i < len; i++)
{
for (int j = ; j < len; j++)
{
ans[i][j] = '#';
}
}
int i = , col = ;
while (i < len)
{
         //竖列
for (int j = ; j < numRows; j++)
{
ans[j][col] = s[i++];
if (i >= len)
break;
}
         //斜列
for (int j = numRows - ; j >= ; j--)
{
if (i >= len)
break;
ans[j][++col] = s[i++];
}
col++;
if (i >= len)
break;
}
s = "";
       //行遍历,得结果
for (int i = ; i < len; i++)
{
for (int j = ; j < len; j++)
{
if (ans[i][j] != '#')
s += ans[i][j];
}
}
return s;
}
};
 //思路2:官方题解
//1. 把每一行当作一个字符串进行处理,vector<string> 当作容器;
//2. 遍历字符串,为各行字符串添加元素;
//3. 行序号的变换(+1/-1)依靠一个bool变量控制,每当到达第一行/最后一行,bool变量取反
//时间:O(n)
//空间:O(n)
1 class Solution {
public:
string convert(string s, int numRows) {
if(numRows == )
return s;
int len = s.length();
vector<string> rows(min(numRows, len));
bool change = false;
int currow = ;
       //C++ 11的写法
for(char c : s)
{
rows[currow] += c;
if(currow == || currow == numRows-)
change = !change;
currow += (change) ? : -;
}
s = "";
for(string per : rows)
s += per; return s;
}
};
//真的

string leetcode-6.ZigZag的更多相关文章

  1. LeetCode 6 ZigZag Conversion 模拟 难度:0

    https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in ...

  2. leetcode 6. ZigZag Conversion

    https://leetcode.com/problems/zigzag-conversion/ 题目: 将字符串转化成zigzag模式. 例如 "abcdefghijkmlnpq" ...

  3. LeetCode 6 ZigZag Conversion(规律)

    题目来源:https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is writt ...

  4. [LeetCode][Python]ZigZag Conversion

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/zigzag- ...

  5. LeetCode——6. ZigZag Conversion

    一.题目链接:https://leetcode.com/problems/zigzag-conversion/description/ 二.题目大意: 给定一个字符串和一个数字,将其转换成Zigzag ...

  6. 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]

    题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...

  7. [LeetCode 题解]: ZigZag Conversion

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 The string ...

  8. [LeetCode] 6. ZigZag Conversion 之字型转换字符串

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  9. [LeetCode] 6. ZigZag Converesion 之字型转换字符串

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  10. 【leetcode】ZigZag Conversion

    题目简述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...

随机推荐

  1. 浅谈-对modbus的理解

    浅谈-对modbus的理解 一.简介 Modbus由MODICON公司于1979年开发,是一种工业现场总线协议标准.1996年施耐德公司推出基于以太网TCP/IP的Modbus协议:ModbusTCP ...

  2. django中的多级评论

    需求分析 一般论坛中有评论和子评论,这样很容易就成了一个评论树,比如以下情况,先看数据结构: #nid,评论内容,跟帖对象(None为根评论) (1, '111', None), (2, '222', ...

  3. Day4作业:蛋疼CRM系统

    先上流程图,还得27寸4K显示器,画图各种爽: ReadMe: 运行程序前的提示: 1.抱歉,你得装prettytable模块...... 2.还得抱歉,如果shell中运行,最好把字体调得小点,表格 ...

  4. PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)

    1040 Longest Symmetric String (25 分)   Given a string, you are supposed to output the length of the ...

  5. vs2012编译的程序不能在XP和2003下执行问题的解决方法

    问题如题,通过无数次百度和谷歌后,发现,微软已经确认这是一个缺陷,安装Vs2012的update 3的升级包就可以解决问题.同时,在分发包的地方,vcredist_x86.exe 随程序分发一份就可以 ...

  6. 【Leetcode_easy】1078. Occurrences After Bigram

    problem 1078. Occurrences After Bigram 题意 solution: class Solution { public: vector<string> fi ...

  7. 【Leetcode_easy】605. Can Place Flowers

    problem 605. Can Place Flowers 题意: solution1: 先通过简单的例子(比如000)发现,通过计算连续0的个数,然后直接算出能放花的个数,就必须要对边界进行处理, ...

  8. Docker镜像仓库Harbor搭建

    园子里面已经有人写过了.也写得很好,我这里只记录下我遇到的问题 Harbor 依赖: 1:docker 2:docker-compose 怎么安装就不说了. 然后是安装Harbor github:ht ...

  9. Selenium ? 也要学...!

    一.selenium 简介 Selenium是ThroughtWorks公司一个强大的开源Web功能测试工具系列,包括Selenium-IDE.Selenium-RC.Selenium-Webdriv ...

  10. java连接zookeeper实现zookeeper的基本操作

    Java服务端连接Zookeeper,进行节点信息的获取,管理…,整理成一个基本工具, 添加依赖: <dependency> <groupId>org.apache.zooke ...