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 ...
随机推荐
- 入门python:《Python编程从入门到实践》中文PDF+英文PDF+代码学习
入门python推荐学习久负盛名的python入门书籍<Python编程从入门到实践>. 书中涵盖的内容是比较精简的,没有艰深晦涩的概念,最重要的是每个小结都附带有"动手试一试& ...
- 转载 :Linux有问必答:如何在Debian或Ubuntu上安装完整的内核源码
http://linux.cn/article-5015-1.html 问题:我需要为我的Debian或Ubuntu下载并安装完整树结构的内核源码以供编译一个定制的内核.那么在Debian或Ubunt ...
- PHP DES-ECB加密对接Java解密
最近公司有个业务,需要对接第三方接口,但是参数是需要加密的,对方也只提供了一个java的demo,在网上到处搜索,没有找到直接就能用的方法,后来还是跟公司的Android工程师对接出来的,在这里记录一 ...
- 使用GitHub+Hexo建立个人网站,并绑定自己的域名(Ubuntu环境下)
参考链接: youngzn.github.io hexo官网 博客:从jekyll到hexo hexo建站小结 全过程 简洁过程 使用GitHub+Hexo建立个人网站,并绑 ...
- HDU 4960 Another OCD Patient 简单DP
思路: 因为是对称的,所以如果两段是对称的,那么一段的前缀和一定等于另一段的后缀和.根据这个性质,我们可以预处理出这个数列的对称点对.然后最后一个对称段是从哪里开始的,做n^2的DP就可以了. 代码: ...
- Android TextureView简易教程
如果你想显示一段在线视频或者任意的数据流比如视频或者OpenGL 场景,你可以用android中的TextureView做到. TextureView的兄弟SurfaceView 应用程序的视频或者o ...
- 35.QQ大数据模型
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <stri ...
- POJ 3265 DP
思路: f[i][j]表示前i天能做j道题 (是做 不是做完) if(f[i-1][k]) if(suma[j]-suma[k]+g[i-1][k]<=n) f[i][j]=1,g[i][j]= ...
- Python(六) Python 函数
一.认识函数 help(方法名字) help(round) 1.功能性 2.隐藏细节 3.避免编写重复的代码 4.组织代码 自定义函数 二.函数的定义及运行特点 # 递归 def sum_num(n ...
- 学习推荐《零起点Python大数据与量化交易》中文PDF+源代码
学习量化交易推荐学习国内关于Python大数据与量化交易的原创图书<零起点Python大数据与量化交易>. 配合zwPython开发平台和zwQuant开源量化软件学习,是一套完整的大数据 ...