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. LeetCode.868-二进制距离(Binary Gap)

    这是悦乐书的第333次更新,第357篇原创 01看题和准备 今天介绍的是LeetCode算法题中Easy级别的第203题(顺位题号是868).给定正整数N,找到并返回N的二进制表示中两个连续1之间的最 ...

  2. mysql常用知识点之limit

    limit函数的应用.limit后面跟整数,如limit 5,表示在结果集中取前5条:limit后跟整数区间,如limit 2,5,表示在结果集中 从第3条开始,取5条数据,第一个整数表示结果集的顺序 ...

  3. 第九周总结&第七次实验报告

    实验7 实验任务详情: 完成火车站售票程序的模拟. 要求: (1)总票数1000张: (2)10个窗口同时开始卖票: (3)卖票过程延时1秒钟: (4)不能出现一票多卖或卖出负数号票的情况. 实验过程 ...

  4. 浅拷贝&深拷贝

    浅拷贝新的对象指向原来对象的地址 深拷贝新的对象中,原来是可变对象,会新复制一份值指向新的地址[11,22,33]若原来的对象里含有可变对象,里面的这个可变对象也会指向新的地址['qwer', 123 ...

  5. Quartz-第四篇 常规quartz的使用

    1.目录结构 2.主要文件 1>引入的jar包,quartz-2.2.2解压后lib下所有的jar包 2>quartz.properties org.quartz.threadPool.t ...

  6. [LeetCode] 137. 只出现一次的数字 II

    题目链接 : https://leetcode-cn.com/problems/single-number-ii/ 题目描述: 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现了三 ...

  7. DotNetCore知识栈

    #..NET Core提供的特性 1.开源.免费 2.轻量级.跨平台 3.组件化.模块化.IOC+Nuget.中间件 4.高性能 5.统一了MVC和WebAPI编程模型 a)  比如:ASP.NET ...

  8. Windows及Android倍速播放视频软件下载

    目录 1. 更多推荐 2. 关键字 3. 按 4. 软件下载 4.1. IOS应用商店 4.2. 网盘下载 5. 软件介绍 5.1. PotPlayer(Windows) 5.2. MoboPlaye ...

  9. CRMEasy知识库访问权限

    知识库设置BCCBINFO文件夹为共享文件夹,给予普通域用户和管理员域用户完全控制权限,但是当以管理员域用户身份对文件进行操作后,以普通域用户登录不会有效果,甚至出现“访问权限”的错误,这是由于文件夹 ...

  10. RMQ 区间最大值最小值 最频繁次数

    区间的最大值和最小值 #include <cstdio> #include <cstring> #include <cmath> #include <iost ...