string leetcode-6.ZigZag
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的更多相关文章
- LeetCode 6 ZigZag Conversion 模拟 难度:0
https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in ...
- leetcode 6. ZigZag Conversion
https://leetcode.com/problems/zigzag-conversion/ 题目: 将字符串转化成zigzag模式. 例如 "abcdefghijkmlnpq" ...
- LeetCode 6 ZigZag Conversion(规律)
题目来源:https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is writt ...
- [LeetCode][Python]ZigZag Conversion
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/zigzag- ...
- LeetCode——6. ZigZag Conversion
一.题目链接:https://leetcode.com/problems/zigzag-conversion/description/ 二.题目大意: 给定一个字符串和一个数字,将其转换成Zigzag ...
- 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]
题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- [LeetCode 题解]: ZigZag Conversion
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 The string ...
- [LeetCode] 6. ZigZag Conversion 之字型转换字符串
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- [LeetCode] 6. ZigZag Converesion 之字型转换字符串
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【leetcode】ZigZag Conversion
题目简述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...
随机推荐
- Mysql视图介绍
视图是一个存在于数据库中的虚拟表.视图本身没有数据,只是通过执行相应的select语句完成获得相应的数据.可以理解为select语句的别名. (1).视图的作用 1.如果某个查询结果出现的非常频繁,即 ...
- PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)
1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime ...
- LeetCode_69. Sqrt(x)
69. Sqrt(x) Easy Implement int sqrt(int x). Compute and return the square root of x, where x is guar ...
- laravel php门面模式
门面模式 理解3个概念: 1)Container的概念,laravel所有的服务都注册在container里面,至于如何注册,就是使用service provider 2)service pr ...
- web前端学习路程
学习路程: 1.HTML和CSS基础 2.JavaScript语言 3.jQuery and ajax 4.综合网站实践 5.优化及调试
- 上下文管理器之__enter__和__exit__
目录 前言 with as是如何工作的 自定制open方法 更多的示例 返回主目录 前言 回到顶部 有个学生在第四轮面试中被CTO问到:如何自定义实现with open的功能.然后就一脸懵逼的回来找我 ...
- dede织梦5.7的安全防护设置
dede安全是一直令人堪忧的,但是其用来建网站很方便,如果我们使用dede来建站,一定要做好安全防护工作. 下面总结一下dede织梦5.7的安全防护设置 1.更改管理员名称和密码,尽可能设置的复杂一下 ...
- cocos creator 倒计时code
let countDownNode = this.viewNode.getChildByName('countDownNode').getComponent(cc.Label); countDownN ...
- 【转载】49个CSS知识点
01.[负边距]
- Java基础---JavaJShell脚本工具
JShell脚本工具是JDK9的新特性 什么时候会用到 JShell 工具呢,当我们编写的代码非常少的时候,而又不愿意编写类,main方法,也不愿意去编译和运行,这个时候可以使用JShell工具. 启 ...