一.题目

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

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,...。

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





三.实现代码

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的更多相关文章

  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. 认识Vue组件

    前言 Vue.js是一套构建用户界面的渐进式框架(官方说明).通俗点来说,Vue.js是一个轻量级的,易上手易使用的,便捷,灵活性强的前端MVVM框架.简洁的API,良好健全的中文文档,使开发者能够较 ...

  2. DBCP2配置详细说明(中文翻译)

    http://blog.csdn.net/kerafan/article/details/50382998 common-dbcp2数据库连接池参数说明 由于commons-dbcp所用的连接池出现版 ...

  3. ArcGIS api for javascript——地图配置-滑动器的刻度线、方向、大小的改变

    描述 本例展示了如果删除缩放等级滑动器的刻度线.通过设置esriConfig里的sliderLabel为null来实现: esriConfig.defaults.map.sliderLabel = n ...

  4. 隐马尔科夫模型(HMM)

    基本概念 1Markov Models 2Hidden Markov Models 3概率计算算法前向后向算法 1-3-1直接计算 1-3-2前向算法 1-3-3后向算法 4学习问题Baum-Welc ...

  5. 用react native 做的一个推酷client

    用react native 做的一个推酷client 仅供大家參考.仅仅为抛砖引玉.希望大家能以此来了解react.并编写出很多其它的优质的开源库,为程序猿做出贡献. 用的的组件: Navigator ...

  6. KVM硬件辅助虚拟化之 EPT in Nested Virtualization

    在嵌套虚拟环境(Nested Virtualization)下,执行在hypervisor上的Virtual Machine仍能够作为hypervisor去执行其他的Virutal Machine,而 ...

  7. Using a Plugin

    创建hello.world <?xml version="1.0"?> <sdf version="1.4"> <world na ...

  8. numpy 数据类型与 Python 原生数据类型

    查看 numpy 数据类型和 Python 原生数据类型之间的对应关系: In [51]: dict([(d, type(np.zeros(1,d).tolist()[0])) for d in (n ...

  9. 分享一个vueui axios-mock-adapter 中的用法

    import axios from 'axios'; import MockAdapter from 'axios-mock-adapter'; import { LoginUsers, Users ...

  10. 浅谈 C 语言中模块化设计的范式

    今天继续谈模块化的问题.这个想慢慢写成个系列,但是不一定连续写.基本是想起来了,就整理点思路出来.主要还是为以后集中整理做点铺垫. 我们都知道,层次分明的代码最容易维护.你可以轻易的换掉某个层次上的某 ...