【一天一道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 ...
随机推荐
- log file sync 等侍值高的一般通用解决办法
log file sync等待时间发生在redo log从log buffer写入到log file期间. 下面对log file sync做个详细的解释. 何时发生日志写入: 1.commit或者r ...
- Ribbon WorkBench 当ValueRule的值为空时的设置
在定制Ribbon按钮的规则的时候,有时需要根据某个字段值是否为空不设定Ribbon按钮的Display rules或Enable Rules,根据Crm的版本的不同,设置有所差别: 对于Dynami ...
- Kafka系列之-Kafka入门
接下来的这些博客,主要内容来自<Learning Apache Kafka Second Edition>这本书,书不厚,200多页.接下来摘录出本书中的重要知识点,偶尔参考一些网络资料, ...
- Java基本语法-----java流程控制语句
1顺序语句 语句:使用分号分隔的代码称作为一个语句. 注意:没有写任何代码只是一个分号的时候,也是一条语句,称作空语句. 顺序语句就是按照从上往下的顺序执行的语句. 2判断(if-else) 在我们找 ...
- Spring之Core模块
Core模块主要的功能是实现了控制反转与依赖注入.Bean配置以及加载.Core模块中有Beans.BeanFactory.BeanDefinitions.ApplicationContext等概念 ...
- Dynamics CRM2015 Update1 新功能之表单增强功能
CRM2015 Update 1发布后,系统的界面的变化很大,仔细观察后会发现表单窗体也有些不同了,在CRM2015 Update1的官方介绍中对此变化的解释是起用了新的窗体呈现引擎,让界面更好看加载 ...
- linux的 压缩与解压 命令集
bzip2压缩费时但效果好,而且支持hadoop的hdfs文件切分,gzip不行 bzip2 [-cdz] 文件名 -c :将压缩的过程输出到屏幕 -d :解压缩 -z :压缩 -# :压缩比的参数, ...
- Android JavascriptBridge 详解(二)
原文出自:http://blog.csdn.net/sk719887916/article/details/47189607 Android开发目前现状来说,开发者大部分时间花在UI的屏幕适配上,使用 ...
- 【java集合框架源码剖析系列】java源码剖析之java集合中的折半插入排序算法
注:关于排序算法,博主写过[数据结构排序算法系列]数据结构八大排序算法,基本上把所有的排序算法都详细的讲解过,而之所以单独将java集合中的排序算法拿出来讲解,是因为在阿里巴巴内推面试的时候面试官问过 ...
- [java面试]宇信易诚 广州分公司 java笔试题目回忆录
本文地址:http://blog.csdn.net/sushengmiyan/article/details/28479895 作者:sushengmiyan -------------------- ...