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 ...
随机推荐
- osg define shape(create box)
#ifdef _WIN32 #include <Windows.h> #endif // _WIN32 #include <osgViewer/Viewer> #include ...
- hadoop目录命令
下面是经常使用到的,以此记录备忘 1.查看hadoop目录 命令: hadoop fs -ls / 2.创建目录 命令:hadoop fs -mkdir /目录名 3.将文件上传hadoop中(也就是 ...
- confluent kafka connect remote debugging
1. Deep inside of kafka-connect start up To begin with, let's take a look at how kafka connect start ...
- ABAP编辑器输入中文变成问号
在ABAP编辑器里输入汉字,点击空格后显示问号? 中英文环境下编辑都出现乱码 实用程序->设置 ->基于文本的编辑器 如果用老式编辑器,可以输入中文 试试打个补丁 GUI740 补丁17 ...
- LeetCode_108. Convert Sorted Array to Binary Search Tree
108. Convert Sorted Array to Binary Search Tree Easy Given an array where elements are sorted in asc ...
- 基于Spark的用户分析系统
https://blog.csdn.net/ytbigdata/article/details/47154529
- easyui-datagrid配置宽度高度自适应
在style属性中,去除之前添加的width和height属性(如果有的话),然后添加"fit:false"即可.
- javascript——URI的编解码方法
有效的URI(统一资源标示符)是不能包含某些字符的,如空格,所以需要进行编码,编码方法有:encodeURI()和encodeURIComponent(), 对编的码进行解码方法有:decodeURI ...
- iOS-UIAlertView与UIActionSheet
UIAlertView与UIActionSheet 6.11.1 常规调用 UIAlertView:调出一个模态对话框,屏幕居中显示 UIActionSheet:非模态对话框,屏幕下方弹出 Alert ...
- 斑马打印机ZBL语言
ZBL手册:https://pan.baidu.com/s/1I8DaMUlf-9ytUwqtURw8rw 下面是打印CODE128条形码的代码 ^XA^FO100,100^BY6 ...