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. ios排序NSArray(数字.字符串)

    NSArray *originalArray = @[@"1",@"21",@"12",@"11",@"0&q ...

  2. 雨田家园 delphi 拆分字符串

    最近在使用Delphi开发一种应用系统的集成开发环境.其中需要实现一个字符串拆分功能,方法基本原型应该是:procedure SplitString(src: string ; ch: Char; v ...

  3. 第九章 JSP标签——《跟我学Shiro》

    转发地址:https://www.iteye.com/blog/jinnianshilongnian-2026398 博客分类: 跟我学Shiro 跟我学Shiro  目录贴:跟我学Shiro目录贴 ...

  4. 游戏开发中伪随机正态分布JavaScript

    在游戏开发中经常遇到随机奖励的情况,一般会采取先生成数组,再一个一个取的方式发随机奖励. 下面是js测试正态分布代码: <!DOCTYPE html> <html lang=&quo ...

  5. python argparse库

    argparse用法总结 https://blog.csdn.net/qq_24551305/article/details/90155858 args = parse.parse_args()par ...

  6. 电子防抖(EIS)无效的相关修改

    [DESCRIPTION] 电子防抖(EIS)无效的相关修改 [SOLUTION] 电子防抖(EIS)无效,根据不同的版本,可以先查看是否已经做了相关修改.1. MT6580/MT6735平台请参考如 ...

  7. hadoop--Unable to load native-hadoop library for your platform解决方法

    笔者实验环境:centos 7.4.1708,hadoop-2.6.0-cdh5.14.2. 执行hadoop命令时出现以下告警,不能加载相关库: WARN util.NativeCodeLoader ...

  8. Ribbon【负载均衡策略】

    ribbon有7种负载均衡策略可供选择: 策略类 命名 描述 RandomRule 随机策略 随机选择server RoundRobinRule 轮询策略 按照顺序选择server(ribbon默认策 ...

  9. destoon 增删改查

    switch($action) { case 'add': //添加页面 if($submit) { // 不允许重名,直接添加时 $old = $db->get_one("SELEC ...

  10. spring 框架的核心总结

    最近在学习Java语言,从而也学习了SpringFramework 这个大名鼎鼎的框架.从而做一些的记录. 题外话: 学习过几种不同的语言,后来知道所有的编程语言里所有的概念翻来覆去都是一样的事物,只 ...