【LeetCode】006. 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"
.
题解:
没有任何技巧,就是找规律。我们将列中元素数等于行数的列称为主列。
由例子可以看出,除了第一行和最后一行,每一行的主列之间都会有一个值,关键就是在于找出这个值的某种规律。
主列之间的位置差值(L 和 A)为 2*numRows - 2。中间值与同处此行中间值之前的元素(P 和 A)的位置差值为 2*numRows - 2 - 2 * 此元素所在的行(以0为起始)
0 6 C
1 5 7 B D
2 4 8 A E
3 9 F
对于主列之间的位置差值。只需找到第一行主列之间的位置差值即可,因为主列从第一行到最后一行元素的位置坐标同步增长。对于行数为 n 的情况,某一主列的元素数显然等于行数 n,此主列到下一个主列之间共有 n - 2 个中间元素(减去的2是因为第一行和最后一行没有中间元素),故主列之间的位置差值为 2n - 2
对于中间元素的位置确定
x-(2n-2) x i = 0 差值 无
` x-1 x+1 i = 1 2
` x-2 x+2 i = 2 4
` x-3 x+3 i = 3 6
` ······································
x-(n) x-(n-2) x+(n-2) i = n-2 2(n-2)
x-(n-1) x+(n-1) i = n-1 无
可见对于某一行 i,主列与中间元素的差值随着行数的增加而翻倍,具体为 差值 = 2 * i
class Solution {
public:
string convert(string s, int numRows) {
if (numRows < )
return s;
string res;
int n = s.size();
int len = * numRows - ;
for (int i = ; i < numRows; ++i) {
for (int j = i; j < n; j += len) {
res += s[j];
int mid = j + len - * i;
if (i != && i != numRows - && mid >= && mid < n)
res += s[mid];
}
} return res;
}
};
【LeetCode】006. ZigZag Conversion的更多相关文章
- 【LeetCode】6. ZigZag Conversion Z 字形变换
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字形变换,ZigZag,题解,Leetcode, 力扣,P ...
- 【LeetCode】6 - ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【LeetCode】6. ZigZag Conversion 锯齿形转换
题目: 思路: 以图为例:s={'A','B','C','D','E','F','G','H'.....} 1.先不考虑中间元素F.G.H.N...,每一行前后元素在数组中对应下标相差size=2*n ...
- 【一天一道LeetCode】#6 ZigZag Conversion
一天一道LeetCode系列 (一)题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given ...
- 【LeetCode】281. Zigzag Iterator 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 deque 日期 题目地址:https://leetc ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
随机推荐
- OpenGL学习进程(5)第三课:视口与裁剪区域
本节是OpenGL学习的第三个课时,下面介绍如何运用显示窗体的视口和裁剪区域: (1)知识点引入: 1)问题现象: 当在窗体中绘制图形后,拉伸窗体图形形状会发生变化: #include ...
- 【Head First Servlets and JSP】笔记4:HttpServletRequest req
api:https://tomcat.apache.org/tomcat-5.5-doc/servletapi/ 1.GET和POST除去数据大小之外的区别. 安全性问题.使用GET的话,参数数据会出 ...
- INSPIRED启示录 读书笔记 - 第35章 情感接纳曲线
技术接纳曲线 涉及了技术创新者.尝鲜者.早期消费大众.后期消费大众和跟随者,很少有产品能越过鸿沟——获得尝鲜者以外消费者的青睐 不同类型的用户具有不同的情感需求,除了技术接纳曲线模型描述用户外,还应该 ...
- mongodb GridFS django FileFiled 默认 widget 只有一个文件上传框显示不了字段内容,重写widget
首先,定位到:FileFiled 默认 widget 源码:mongoadmin包options.py中,如下: # Defaults for formfield_overrides. ModelAd ...
- SpringMVC传递参数和获取参数以及返回数据
1.传递form表单,参数接收到对象,name和对象属性对应上即可: 2.springmvc不能直接通过form表单传递多个对象的list集合,要么采用ajax传递,要么采用封装了list属性的b ...
- Hive数据类型总结
转载自:http://blog.csdn.net/chenxingzhen001/article/details/20901045 Hive的内置数据类型可以分为两大类:(1).基础数据类型:(2). ...
- Solr新建collection时报错 Caused by: Direct buffer memory
错误如下 [root@192.168.1.235 conf]# curl "http://192.168.1.235:8983/solr/admin/collections ?action= ...
- R树的相关知识
转自:http://blog.csdn.net/houzuoxin/article/details/16113895 R树在数据库等领域做出的功绩是非常显著的.它很好的解决了在高维空间搜索等问题.举个 ...
- review09
String类在java.lang包中,由于java.lang包中的类被默认引入,所以可以直接使用String类.String对象的创建可以直接使用带字符串参数的构造方法 String s = new ...
- PowerDesigner15生成数据库 同时自动生成字段说明(备注)信息
1.打开Database->Generate Database 2.切换到Format标签页,选中Generate name in empty comment即可生成每个字段的说明(备注)信息 ...