6. ZigZag Conversion
Medium

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 (之字型)
 class Solution {
public:
string convert(string s, int numRows) {
int len = s.length();
string ans;
if(numRows==) return s;
for(int i = ;i < numRows; i++){
int left = (numRows--i)*;
int right = (i)*;
int cnt = i;
int flag = ;
while(cnt < len){
flag = (flag+)%;
if((left== && flag==)||(right==&&flag==)) continue;
ans+=s[cnt];
if(flag==) cnt = cnt+left;
else cnt = cnt+right;
}
}
return ans;
}
};

Leetcode 6. ZigZag Conversion(找规律,水题)的更多相关文章

  1. 长春理工大学第十四届程序设计竞赛F Successione di Fixoracci——找规律&&水题

    题目 链接 题意:给出x数列的定义: $T_0 = a$ $T_1 = b$ $T_n = T_{n-2} \bigoplus T_{n-1} $ 求第 $n$ 项( $0 \leqslant a,b ...

  2. LeetCode 6 ZigZag Conversion(规律)

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

  3. Gym 101194A / UVALive 7897 - Number Theory Problem - [找规律水题][2016 EC-Final Problem A]

    题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...

  4. Codeforces Round #259 (Div. 1) A. Little Pony and Expected Maximum 数学公式结论找规律水题

    A. Little Pony and Expected Maximum Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.c ...

  5. 【leetcode】ZigZag Conversion

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

  6. LeetCode——6. ZigZag Conversion

    一.题目链接:https://leetcode.com/problems/zigzag-conversion/description/ 二.题目大意: 给定一个字符串和一个数字,将其转换成Zigzag ...

  7. LeetCode 6. ZigZag Conversion & 字符串

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

  8. LeetCode 06 ZigZag Conversion

    https://leetcode.com/problems/zigzag-conversion/ 水题纯考细心 题目:依照Z字形来把一个字符串写成矩阵,然后逐行输出矩阵. O(n)能够处理掉 记i为行 ...

  9. Leetcode 6 ZigZag Conversion 字符串处理

    题意:将字符串排成Z字形. PAHNAPLSIIGYIR 如果是5的话,是这样排的 P     I AP   YR H L G N  SI A    I 于是,少年少女们,自己去找规律吧 提示:每个Z ...

随机推荐

  1. CNN(卷积神经网络)原理讲解及简单代码

    一.原理讲解 1. 卷积神经网络的应用 分类(分类预测) 检索(检索出该物体的类别) 检测(检测出图像中的物体,并标注) 分割(将图像分割出来) 人脸识别 图像生成(生成不同状态的图像) 自动驾驶 等 ...

  2. 文件压缩、解压工具类。文件压缩格式为zip

    package com.JUtils.file; import java.io.BufferedOutputStream; import java.io.File; import java.io.Fi ...

  3. 将python 2.6 升级到 2.7,及pip安装

    由于CentOS6.5 自带python版本为2.6.6,实际中使用的大多为2.7.x版本.于是手动升级. 查看python的版本 #python -VPython 2.6.6 1.下载Python- ...

  4. 转 appium grid分布式环境搭建

    https://blog.csdn.net/ljl6158999/article/details/80803239 说起grid,了解selenium的人肯定知道,他就是分布式的核心.原理是简历中心h ...

  5. 创建React脚手架

    node版本10.14.2 下载地址 如果是其版本的话会出错 css-loader 会不兼容 主要是8.x的版本不兼容 npm install -g create-react-app 全局安装 cre ...

  6. PHP 中一个 False 引发的问题,差点让公司损失一百万

    PHP 中一个 False 引发的问题,差点让公司损失一百万 一.场景描述 上周我一个在金融公司的同学,他在线上写一个 Bug,差点造成公司损失百万.幸好他及时发现了这个问题并修复了.这是一个由 PH ...

  7. Django验证码实现

    1.点击验证码更换新的验证码 2.验证码必须是图片形式的 3.验证码实现的流程 服务端: a. session中保存随机验证码,如:87fs b.把验证码写到一个白板里面制作成图片 c. 在页面中显示 ...

  8. C#导出大量数据到excel,怎么提升性能

    一,要提升性能,我们先要知道耗时的地方在哪里 1,数据库查询,2,把数据组合成新集合循环嵌套太多 二,那我们怎么优化呢? 一,数据库查询,1>,数据库查询:如果数据量小,我们可以用临时datat ...

  9. vue中获取滚动table的可视页面宽度,调整表头与列对齐(每列宽度不都相同)

    mounted() { // 在mounted中监听表格scroll事件 this.$refs.scrollTable.addEventListener( 'scroll',(event) => ...

  10. CABasicAnimation animationWithKeyPath Types

    转自:http://www.cnblogs.com/pengyingh/articles/2379631.html CABasicAnimation animationWithKeyPath 一些规定 ...