【Leetcode】【Easy】ZigZag Conversion
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".
解题:
引用http://blog.csdn.net/zhouworld16/article/details/14121477的图片说明:
所谓的ZigZag,就是这样的:


因此可以找到每行排列数字的规律,nRows = n,字符串字符下标从0开始。
普通青年:
① 先读第1行:0, 0 + 2n-2, 0 + (2n-2)*2,...
② 读第2行至第n-2行,设行数为e,则每行读取数字为:
e-1,e-1 + 2n-2 - (e-1)*2, e-1 + 2n-2, e-1+2n-2 + 2n-2 - (e-1)*2, ...
规律即,两个数字一组,每组间隔2n-2:
for (int i=e-1; i<string.length(); i+=2n-2) {
每组元素可表示为i 和 i + 2n-2 - (e-1)*2
}
③ 读最后一行:n-1, n-1 + 2n-2, n-1 + (2n-2)*2, ...
(由于每循环一组,会读出两个数字。注意判定字符串越界)
class Solution {
public:
string convert(string s, int nRows) {
string res;
int len = s.length();
if (nRows == )
return res;
if (nRows == )
return s;
for(int i=; i<len; i+=*nRows-)
res.push_back(s[i]);
int index = ;
while(nRows--index){
for(int i=index; i<len; i+=*nRows-) {
res.push_back(s[i]);
int zig = i + 2 * nRows - 2 * index - ;
if (zig < len)
res.push_back(s[zig]);
}
index++;
}
for(int i=nRows-; i<len; i+=*nRows-)
res.push_back(s[i]);
return res;
}
};
聪明的青年:
类似于期末考试考场排座位,一般为蛇形排号。将考场的座位按一竖列为一组,编号时,第一组的座位从前向后编号,对第二组从后向前进行编号,如下图:
① ⑧ ⑨
② ⑦ ...
③ ⑥
④ ⑤
nRows即为每组能坐多少人。因此先让同学们按编号入座,再从前向后按横排依次读出他们的编号即为本题答案。
class Solution {
public:
string convert(string s, int nRows) {
string sArray[nRows];
string res = "";
int index = ;
if (nRows == )
return s;
for(int i=; i<s.length(); ++i){
sArray[index] += s[i];
if (!(i / (nRows-) % )) {
index++;
} else {
index--;
}
}
for(int i=; i<nRows; ++i){
res += sArray[i];
}
return res;
}
};
当然是原创的。
【Leetcode】【Easy】ZigZag Conversion的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【LeetCode算法题库】Day2:Median of Two Sorted Arrays & Longest Palindromic Substring & ZigZag Conversion
[Q4] There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of th ...
- 【leetcode刷题笔记】ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number
[Q7] 把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...
- 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists
[Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...
- 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman
[Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...
- 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters
[Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...
随机推荐
- 洛谷 P4093 [HEOI2016/TJOI2016]序列(Cdq+dp)
题面 luogu 题解 \(Cdq分治+dp\) \(mx[i],mn[i]\)分别表示第\(i\)位最大,最小能取到多少 那么有 \(j < i\) \(mx[j] \le a[i]\) \( ...
- Tensorflow基础-mnist数据集
MNIST数据集,每张图片包含28*28个像素,把一个数组展开成向量,长度为28*28=784,故数据集中mnist.train.images是一个形状为[60000,784]的张量,第一个维度数字用 ...
- 2018acm-icpc青岛站心得
今年总共两场区域赛,一场南京,一场青岛.南京场队伍真正开始磨合,虽然最后还是铜牌,但是和银牌队伍其实只差一个计算几何的板子的问题.而鉴于南京的教训,所以在准备青岛站的时候,我准备了非常多的模板,还和派 ...
- WEB图表制作
https://www.hcharts.cn/demo/highcharts/column-drilldown
- UltraEdit 21.3 增加 mssql, json 高亮
1. %appdata%\IDMComp\UltraEdit 2. 将msql2k.uew, json.uew 放到 wordfiles 目录即可 msql2k json的uew 下载地址如 ...
- 【3dsMax安装失败,如何卸载、安装3dMax 2016?】
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- Java关键字final、static使用总结 (final static在容器中不可以改变容器但可以改变存放)
一.final 根据程序上下文环境,Java关键字final有“这是无法改变的”或者“终态的”含义,它可以修饰非抽象类.非抽象类成员方法和变量.你可能出于两种理解而需要阻止改变:设计或效 ...
- 牛客网Java刷题知识点之数组、链表、哈希表、 红黑二叉树
不多说,直接上干货! 首先来说一个非常形象的例子,来说明下数组和链表. 上体育课的时候,老师说:你们站一队,每个人记住自己是第几个,我喊到几,那个人就举手,这就是数组. 老师说,你们每个人记住自己前面 ...
- 常用Redis命令
在 Windows 下配置 Redis 集群 在 Windows 下配置多个 Redis(简化配置) MicrosoftArchive/redis 官方Redis集群搭建文档 Redis命令 Cent ...
- 浏览器后退->清除原页面div中填写的数据
需求说明:页面表单用前端用div布局,提交之后跳转到另一个页面,但是考虑到客户奇怪的脑回路,可能会点击浏览器的后退按钮,不知道是个体情况还是都是一样,原本div中填写的数据还依然存在,所以需要让页面在 ...