原题链接

把字符串按照 ↓↗↓……的顺序,排列成一个 Z 形,返回 从左到右,按行读得的字符串。
思路:
建立一个二维数组来按行保存字符串。
按照 ↓↗↓……的方向进行对每一行加入字符。

太慢了这个解法,Runtime: 96 ms, faster than 3.61% of C++。

class Solution
{
public:
string convert(string s, int numRows)
{
if (numRows <= )
return s;
string res;
string zigZag[numRows];
int len = s.length();
int dirc = ; // 1表示方向向下,-1表示方向向上
int index = ;
for (int i = ; i < len; i++)
{
zigZag[index] += s[i];
if (index == numRows - )
dirc = -; if (index == )
dirc = ;
index += dirc;
}
for (int i = ; i < numRows; i++)
{
cout << zigZag[i] << endl;
res += zigZag[i];
} return res;
}
};

[LeetCode] 6. ZigZag Conversion (Medium)的更多相关文章

  1. Leetcode 6. ZigZag Conversion(找规律,水题)

    6. ZigZag Conversion Medium The string "PAYPALISHIRING" is written in a zigzag pattern on ...

  2. LeetCode 6. ZigZag Conversion & 字符串

    ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...

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

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

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

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

  5. LeetCode 6 ZigZag Conversion(规律)

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

  6. [LeetCode][Python]ZigZag Conversion

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

  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】ZigZag Conversion

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

随机推荐

  1. Qt静态编译时使用OpenSSL有三种方式(不使用,动态使用,静态使用,默认是动态使用)

    When compiling Qt you can choose one of these options based on the configure command line: no OpenSS ...

  2. Delphi中文件流的使用方法

    在Delphi中,所有流对象的基类为TStream类, 其中定义了所有流的共同属性和方法.TStream类中定义的属性介绍如下: 1.Size: 此属性以字节返回流中数据大小. 2.Position: ...

  3. 做个知识回顾目录,打算每日更新一下ios的基础知识

    一.基础技能列表:   01 面向对象特性       类与方法封装       通过继承扩展类       抽象类与方法覆盖       多态.动态类型和动态绑定       分类和协议       ...

  4. Oracle PL/SQL编程

    一.PL/SQL简介 1.概念:PL/SQL是Oracle在标准SQL语言上的过程性扩展. 2.优点和特性 提高应用程序的运行性能 提供模块化的程序设计功能 允许定义标示符 具有过程语言控制结构 具备 ...

  5. java关键字-final

    final特点: 1:这个关键字是一个修饰符,可以修饰类,方法,变量. 2:被final修饰的类是一个最终类,不可以被继承. 3:被final修饰的方法是一个最终方法,不可以被覆盖. 4:被final ...

  6. 代码审计之seacms v6.45 前台Getshell 复现分析

    1.环境: php5.5.38+apache+seacms v6.45 seacms目录结构: │─admin //后台管理目录 │ │─coplugins //已停用目录 │ │─ebak //帝国 ...

  7. 微服务SpringCloud之服务注册与发现

    在找.net core 微服务框架时发现了Steeltoe开源项目,它可以基于Spring Cloud实现.net core和.net  Framework的微服务.正好之前也有学习过SpringBo ...

  8. 节能减排到底如何----google earth engine 告诉你!!

    (First,再次严谨说明,本人成果未经允许,切勿发表到相关学术期刊,如果有技术交流,qq1044625113,顺便打个广告,兼职GEE开发,欢迎联系!) 终于过了严寒的冬天,2017年的冬天中国南方 ...

  9. 【Zookeeper01】ubuntu下安装zookeeper单例以及集群

    参考链接:http://zookeeper.apache.org/ https://www.cnblogs.com/lyhc/p/6560993.html 系统: 乌班图16.04 虚拟机(zk一般要 ...

  10. Cisco packet tracer6.0下的网络工程实训

    期末的专业实训,在cisco6.0下配置一个简单的局域网.主要用到了下面几个技术:dhcp中继.vlan的划分.链路聚合.静态nat.ospf协议.访问控制列表.先看一下总的拓扑图,在分步实现功能. ...