一.题目

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)

  1. P A H N
  2. A P L S I I G
  3. 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:

  1. string convert(string text, int nRows);

convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".

Show Tags
Have you met this question in a real interview?

Yes
No

Discuss






















二.解题技巧

    这道题是就是原来的字符串的元素与锯齿化后的字符串的元素之间的关系,我们能够举个样例来说明,如果原来的字符串的每个字符的下标为0,1,2,3。..., 12分别进行行数为3。4,5行的锯齿化后,得到的锯齿化形状例如以下:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2hlbmdfYWk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

    定义将原来的字符串依照nRows行进行锯齿化,定义 Step= 2 * nRows - 2; 从上面的样例能够看出,对于第i行。有以下两种情况:
1.对于第0行和第(nRows - 1)行,每一行的元素为i, i+Step, i+2*Step,...;
2.对于其它的行来说,每一行的元素为i, Step - i, i + Step, 2*Step - i,...。

得到这些映射关系之后,将上面得到锯齿化矩阵进行按行展开,放入到新的字符串中就得到满足要求的新字符串了。





三.实现代码

  1. class Solution {
  2. public:
  3. string convert(string s, int nRows)
  4. {
  5. const int Size = s.size();
  6. if ((Size <= nRows) || (nRows == 1))
  7. {
  8. return s;
  9. }
  10. const int Step = 2 * nRows - 2;
  11. string Result;
  12. for (int RowIndex = 0; RowIndex < nRows; RowIndex++)
  13. {
  14. int Index = RowIndex;
  15. if ((RowIndex == 0) || (RowIndex == (nRows - 1)))
  16. {
  17. while (Index < Size)
  18. {
  19. Result.push_back(s[Index]);
  20. Index = Index + Step;
  21. }
  22. continue;
  23. }
  24. int SecondIndex = Step - Index;
  25. while ((Index < Size) || (SecondIndex < Size))
  26. {
  27. if (Index < Size)
  28. {
  29. Result.push_back(s[Index]);
  30. Index = Index + Step;
  31. }
  32. if (SecondIndex < Size)
  33. {
  34. Result.push_back(s[SecondIndex]);
  35. SecondIndex = SecondIndex + Step;
  36. }
  37. }
  38. }
  39. return Result;
  40. }
  41. };



四.体会

    这道题主要是寻找原来是字符串的元素坐标与锯齿化后的字符串的坐标关系,假设将原始字符串的元素坐标看作是一组已经排好序的数组的话,我们要做的就是怎样将这个数组依照一定规律进行乱序。主要是通过几个样例来推出通用的映射关系,然后依据这些映射关系进行编程就可以。绘图对于解决算法题目还是十分有效的。




版权全部,欢迎转载,转载请注明出处,谢谢







LeetCode_ZigZag Conversion的更多相关文章

  1. 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 ... 这个错误是因为有两个相 ...

  2. 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 ...

  3. 【leetcode】ZigZag Conversion

    题目简述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...

  4. Conversion Operators in OpenCascade

    Conversion Operators in OpenCascade eryar@163.com Abstract. C++ lets us redefine the meaning of the ...

  5. No.006:ZigZag Conversion

    问题: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  6. 错误提示:LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt 的解决方法

    最近在win7 系统下,打算利用 cmake 生成项目文件,然后用vs2010进行编译.但是在cmake的时候出现错误弹窗:

  7. ZigZag Conversion leetcode java

    题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  8. VS2010 LINK1123:failure during conversion to COFF:file invalid or corrupt

    今天用Visual Studio 2010编译一个C工程时突然遇到下面这个编译错误.fatal error LINK1123:failure during conversion to COFF:fil ...

  9. 【leetcode❤python】 6. ZigZag Conversion

    #-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object):    def convert(self, s, numRow ...

随机推荐

  1. 入门python:《Python编程从入门到实践》中文PDF+英文PDF+代码学习

    入门python推荐学习久负盛名的python入门书籍<Python编程从入门到实践>. 书中涵盖的内容是比较精简的,没有艰深晦涩的概念,最重要的是每个小结都附带有"动手试一试& ...

  2. 转载 :Linux有问必答:如何在Debian或Ubuntu上安装完整的内核源码

    http://linux.cn/article-5015-1.html 问题:我需要为我的Debian或Ubuntu下载并安装完整树结构的内核源码以供编译一个定制的内核.那么在Debian或Ubunt ...

  3. PHP DES-ECB加密对接Java解密

    最近公司有个业务,需要对接第三方接口,但是参数是需要加密的,对方也只提供了一个java的demo,在网上到处搜索,没有找到直接就能用的方法,后来还是跟公司的Android工程师对接出来的,在这里记录一 ...

  4. 使用GitHub+Hexo建立个人网站,并绑定自己的域名(Ubuntu环境下)

    参考链接: youngzn.github.io     hexo官网     博客:从jekyll到hexo    hexo建站小结  全过程  简洁过程 使用GitHub+Hexo建立个人网站,并绑 ...

  5. HDU 4960 Another OCD Patient 简单DP

    思路: 因为是对称的,所以如果两段是对称的,那么一段的前缀和一定等于另一段的后缀和.根据这个性质,我们可以预处理出这个数列的对称点对.然后最后一个对称段是从哪里开始的,做n^2的DP就可以了. 代码: ...

  6. Android TextureView简易教程

    如果你想显示一段在线视频或者任意的数据流比如视频或者OpenGL 场景,你可以用android中的TextureView做到. TextureView的兄弟SurfaceView 应用程序的视频或者o ...

  7. 35.QQ大数据模型

    #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <stri ...

  8. 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]= ...

  9. Python(六) Python 函数

    一.认识函数 help(方法名字)  help(round) 1.功能性 2.隐藏细节 3.避免编写重复的代码 4.组织代码 自定义函数 二.函数的定义及运行特点 # 递归 def sum_num(n ...

  10. 学习推荐《零起点Python大数据与量化交易》中文PDF+源代码

    学习量化交易推荐学习国内关于Python大数据与量化交易的原创图书<零起点Python大数据与量化交易>. 配合zwPython开发平台和zwQuant开源量化软件学习,是一套完整的大数据 ...