[LeetCode] 6. ZigZag Conversion (Medium)
把字符串按照 ↓↗↓……的顺序,排列成一个 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)的更多相关文章
- Leetcode 6. ZigZag Conversion(找规律,水题)
6. ZigZag Conversion Medium The string "PAYPALISHIRING" is written in a zigzag pattern on ...
- LeetCode 6. ZigZag Conversion & 字符串
ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...
- 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]
题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- 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/ The string "PAYPALISHIRING" is writt ...
- [LeetCode][Python]ZigZag Conversion
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/zigzag- ...
- [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】ZigZag Conversion
题目简述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...
随机推荐
- autotools工具使用 good
学习GNU/LINUX开发的编程人员,上手之后不久就会在编译开源软件的时候碰到configure脚本,过段时间还会知道configure脚本是 autoconf生成的:但是真正想用起来autoconf ...
- win7访问部分win2003速度慢
解决办法: 关闭TCPIP协议的自动优化调整功能,在win7上,以管理员身份运行cmd,输入 netsh interface tcp set global autotuninglevel=disabl ...
- 获取原生DOM,diy脚手架,vue-clide使用,element-ui的使用
一.获取原生DOM的方式 给标签或者属性添加ref属性 //1.添加属性 <div ref='shy'><div> <Home ref='home'></Ho ...
- Zookeeper详解-安装(四)
ZooKeeper服务器是用Java创建的,它在JVM上运行.你需要使用JDK 6或更高版本. 步骤1:验证Java安装 相信你已经在系统上安装了Java环境.现在只需使用以下命令验证它. $ jav ...
- docker系列(五):网络通信
1 引言 之前的几篇docker系列博客说的都是单个容器或者镜像的操作.但容器,作为一种简化的操作系统,又怎能不与机器或者容器相互协同工作呢,这就需要用到容器的网络功能.docker中提供了多种不同的 ...
- Binary classification - 聊聊评价指标的那些事儿【回忆篇】
在解决分类问题的时候,可以选择的评价指标简直不要太多.但基本可以分成两2大类,我们今分别来说道说道 基于一个概率阈值判断在该阈值下预测的准确率 衡量模型整体表现(在各个阈值下)的评价指标 在说指标之前 ...
- 使用Core Audio实现VoIP通用音频模块
最近一直在做iOS音频技术相关的项目,由于单项直播SDK,互动直播SDK(iOS/Mac),短视频SDK,都会用到音频技术,因此在这里收集三个SDK的音频技术需求,开发一个通用的音频模块用于三个SDK ...
- 【转】三次握手——https为什么更安全
三次握手与四次挥手: https://blog.csdn.net/legend050709/article/details/39804519 https://blog.csdn.net/luoyoub ...
- QT MSVC编译中文乱码问题
两种解决方案: 1,在文件头添加 #if _MSC_VER >= 1600 #pragma execution_character_set("utf-8") #endif 注 ...
- 分析了16年的福利彩票记录,原来可以用Python这么买彩票
目录 0 引言 1 环境 2 需求分析 3 代码实现 4 后记 0 引言 上周被一则新闻震惊到了,<2454万元大奖无人认领!福彩史上第二大弃奖在广东中山产生 >,在2019年5月2日开奖 ...