LeetCode_ZigZag Conversion
一.题目
ZigZag Conversion
Total Accepted: 31399 Total
Submissions: 140315My
Submissions
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"
.
二.解题技巧
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2hlbmdfYWk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
1.对于第0行和第(nRows - 1)行,每一行的元素为i, i+Step, i+2*Step,...;2.对于其它的行来说,每一行的元素为i, Step - i, i + Step, 2*Step - i,...。
得到这些映射关系之后,将上面得到锯齿化矩阵进行按行展开,放入到新的字符串中就得到满足要求的新字符串了。
三.实现代码
class Solution {
public:
string convert(string s, int nRows)
{
const int Size = s.size();
if ((Size <= nRows) || (nRows == 1))
{
return s;
}
const int Step = 2 * nRows - 2;
string Result;
for (int RowIndex = 0; RowIndex < nRows; RowIndex++)
{
int Index = RowIndex;
if ((RowIndex == 0) || (RowIndex == (nRows - 1)))
{
while (Index < Size)
{
Result.push_back(s[Index]);
Index = Index + Step;
}
continue;
}
int SecondIndex = Step - Index;
while ((Index < Size) || (SecondIndex < Size))
{
if (Index < Size)
{
Result.push_back(s[Index]);
Index = Index + Step;
}
if (SecondIndex < Size)
{
Result.push_back(s[SecondIndex]);
SecondIndex = SecondIndex + Step;
}
}
}
return Result;
}
};
四.体会
版权全部,欢迎转载,转载请注明出处,谢谢
LeetCode_ZigZag Conversion的更多相关文章
- Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ...
Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ... 这个错误是因为有两个相 ...
- View and Data API Tips : Conversion between DbId and node
By Daniel Du In View and Data client side API, The assets in the Autodesk Viewer have an object tree ...
- 【leetcode】ZigZag Conversion
题目简述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...
- Conversion Operators in OpenCascade
Conversion Operators in OpenCascade eryar@163.com Abstract. C++ lets us redefine the meaning of the ...
- No.006:ZigZag Conversion
问题: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- 错误提示:LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt 的解决方法
最近在win7 系统下,打算利用 cmake 生成项目文件,然后用vs2010进行编译.但是在cmake的时候出现错误弹窗:
- ZigZag Conversion leetcode java
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- VS2010 LINK1123:failure during conversion to COFF:file invalid or corrupt
今天用Visual Studio 2010编译一个C工程时突然遇到下面这个编译错误.fatal error LINK1123:failure during conversion to COFF:fil ...
- 【leetcode❤python】 6. ZigZag Conversion
#-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object): def convert(self, s, numRow ...
随机推荐
- numpy中list array matrix比较
用python中的numpy包的时候不小心踩了array和matrix的大坑,又引申一下比较list array matrix之间的异同.数据结构(Data Structures)基本上人如其名——它 ...
- 数据迁移工具kettle简单上手
近期做了不少数据迁移工作,无一例外都是kettle做的,对于这些工具,我认为.够用就好,不用做特别多的研究(当然.除非你是这款工具的忠实粉丝,我相信这种没几个).kettle也不例外.在我看来就是不同 ...
- HDU 1730 Northcott Game
简单的尼姆博弈.想到了非常easy! 就拿一行举例.怎么赢.? 就是死劲挨着对方移,当然假设本身就是挨着的,又轮到你移动了,那么对于这一行来讲.你就是输的!! 由此可见每一行的棋子起始距离就显得尤为重 ...
- Select 选择算法 - 编程珠玑(续) 笔记
Select 算法 I 编程珠玑(续)介绍的 Quickselect 算法 选择 N 个元素中的第 K 小(大)值,是日常场景中常见的问题,也是经典的算法问题. 选取 N 个元素的数组的中的第 K 小 ...
- POJ 1191 记忆化搜索
(我是不会告诉你我是抄的http://www.cnblogs.com/scau20110726/archive/2013/02/27/2936050.html这个人的) 一开始没有想到要化一下方差的式 ...
- selenium无法定位元素问题
在用自动化测试工具selenium完成下载任务时,经常会遇到定位不到元素的情况,总结如下: 1.frame/iframe原因定位不到元素: 这个是最常见的原因,首先要理解下frame的实质,frame ...
- 学习参考《零基础入门学习Python》电子书PDF+笔记+课后题及答案
国内编写的关于python入门的书,初学者可以看看. 参考: <零基础入门学习Python>电子书PDF+笔记+课后题及答案 Python3入门必备; 小甲鱼手把手教授Python; 包含 ...
- Nginx缩略图和Fastdfs整合以及image_filter配置,7点经验结论和5个参考资料
以下是7点经验结论和5个参考资料 1.Nginx单独配置缩略图与Nginx和Fastdfs模块整合,配置是不一样的. 非整合模式的配置,类似这样的: location ~* /(\d+)\.(jpg ...
- 【Henu ACM Round#18 B】Modulo Sum
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] m比较小 <=1000 a[i]直接看成a[i]%m就可以了. 有n个0..999之间的整数.. 如果有一个0那么就直接输出Y ...
- GridControl添加右键菜单
private void gridView1_MouseDown(object sender, MouseEventArgs e) { GridHitInfo vi = gridView1.CalcH ...