【一天一道LeetCode】#6 ZigZag Conversion
一天一道LeetCode系列
(一)题目
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 text, int nRows); convert(“PAYPALISHIRING”, 3)
should return “PAHNAPLSIIGYIR”.
题意:
(二)解题
已知输入条件:初始string s和numRows。可以得出循环间隔gap = 2*numRows-2。
统计规律:
- 第0行 从j=0开始,间距gap,取s[j]
- 第1~numRows-2行 从j=1开始,间距gap=(j%gap)<numRows?(gap-2(j%gap)):(2*gap-2(j%gap)),取s[j]
- 第numRows-1行,间距gap,取s[j]
代码如下:
class Solution {
public:
string convert(string s, int numRows) {
string result;
int gap=2*numRows-2; //初始化gap
if(numRows == 1) return s;//rows为1,直接返回
for(int i = 0 ; i < numRows ; i++)
{
int j = i ;
bool flag = true;
if(i == 0 || i == numRows-1)//如果第0行或最后一行
{
int j = i;
while(j<s.length())
{
result+=s[j];
j += gap;//间距为gap
}
}
else{
int j = i;
while(j<s.length()){
result+=s[j];
j += (j%gap)<numRows?(gap-2*(j%gap)):(2*gap-2*(j%gap));
}
}
}
return result;
}
};
【一天一道LeetCode】#6 ZigZag Conversion的更多相关文章
- LeetCode 6. ZigZag Conversion & 字符串
ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...
- Leetcode 6. ZigZag Conversion(找规律,水题)
6. ZigZag Conversion Medium The string "PAYPALISHIRING" is written in a zigzag pattern on ...
- 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——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】ZigZag Conversion
题目简述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...
随机推荐
- ELK搭建
ELK安装 elasticsearch安装 * 下载elasticsearch-5.0.0.tar.gz,并解压. 通过elasticsearch.yml可设置host和port. vim confi ...
- DragVideo,一种在播放视频时,可以任意拖拽的方案
转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/53638896 前言 项目已开源到 ...
- cocos2dx 3.2之Lua打飞机项目
1 创建lua打飞机项目 cocos new T32Lua -dE:\Installed\cocos2d-x-3.2\cocos2d-x-3.2\projects -l lua 2 ...
- x264源代码简单分析:宏块分析(Analysis)部分-帧间宏块(Inter)
===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...
- 分布式缓存GemFire架构介绍
1什么是GemFire GemFire是一个位于应用集群和后端数据源之间的高性能.分布式的操作数据(operational data)管理基础架构.它提供了低延迟.高吞吐量的数据共享和事件分发.Gem ...
- Swift延迟加载的一种用途
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 有以下一种情况: 我们试图用Cocoa的语音合成类NSSpee ...
- Strom数据流分组解析
本文可作为 <<Storm-分布式实时计算模式>>一书1.5节的读书笔记 数据流分组定义了一个数据流中的tuple如何分发给topology中不同bolt的task. Shuf ...
- UNIX环境高级编程——创建孤儿进程
/* 创建孤儿进程 父进程终止后,向子进程发送挂断信号,又接着发送继续信号. */ #include <stdio.h> #include <stdlib.h> #includ ...
- UNIX网络编程——经常使用的套接字选项
1.设置/获取套接字选项 int setsockopt(int socket, int level, int option_name, const void *option_value, sockle ...
- HBase作为存储方案
HBase存储特点 * Client 1. 包含访问HBase的接口,并维护cache来加快对HBase的访问,比如region的位置信息. * Zookeeper: 1. 选举集群中的Master, ...