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 ...
随机推荐
- C++线程互斥、同步
一.线程互斥 如果多个线程需要访问且可能修改同一个变量,那么需要加锁,保证同一时刻只有一个线程可以访问,这个动作即最小“原子操作” 方式1: 使用c++提供的类mutex,lock,unlock即可 ...
- Django中使用Bootstrap----带view.py视图函数(也就是项目下的脚本文件)
一.Django中使用Bootstrap 1.首先建立工程,建立工程请参照:https://www.cnblogs.com/effortsing/p/10394511.html 2.在Firstdja ...
- nginx利用fastcgi_cache模块缓存
nginx不仅有个大家很熟悉的缓存代理后端内容的proxy_cache,还有个被很多人忽视的fastcgi_cache.proxy_cache的作用是缓存后端服务器的内容,可能是任何内容,包括静态的和 ...
- 01.轮播图之二 :tableView 轮播
在做这个tablevew轮播的时候,重要的就是修改frame 和view 的翻转了:::: 也是不难的,概要的设计和scroll 轮播是一致的: 首先是 .h 的文件 @interface Table ...
- Xena L23网络测试仪Valkyrie使用技巧100例:使用Xena官方在线演示设备 (编号00)
需求# 1.新用户:没有硬件,想看看软件长什么样,好不好用,风格如何,怎么办? 2.代理商:没有硬件,想给客户Show一下Xena高大上的软件,怎么办? 3.老用户:邮件推送了新的软件版本,据说很多新 ...
- 最新 思贝克java校招面经 (含整理过的面试题大全)
从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.思贝克等10家互联网公司的校招Offer,因为某些自身原因最终选择了思贝克.6.7月主要是做系统复习.项目复盘.LeetCo ...
- Flink中API使用详细范例--window
Flink Window机制范例实录: 什么是Window?有哪些用途? 1.window又可以分为基于时间(Time-based)的window 2.基于数量(Count-based)的window ...
- (模板)扩展kmp算法(luoguP5410)
题目链接:https://www.luogu.org/problem/P5410 题意:有两个字符串a,b,要求输出b与a的每一个后缀的最长公共前缀.输出: 第一行有lenb个数,为b的next数组( ...
- Windows10下安装numpy
1.https://bootstrap.pypa.io/get-pip.py 下载get-pip.py(右键另存为即可) 2.命令行下在get-pip.py所在文件夹下运行get-pip.py 3.命 ...
- python多任务基础
1.多任务:两个程序段同时运行2.为某个函数创建线程并启动: import threading 线程名 = threading.Thread(target = 函数名,args = 参数元组) #创建 ...