#include<iostream>
//#include<valarray>
#include<vector>
#include<string>
using namespace std;
class Solution {
public:
string convert(string s, int numRows)
{
int h2 = s.length();
int lie1 = h2;
if (numRows != )
lie1 = h2 /2 + ; //得到排列的列数
vector<vector<char>>ret(numRows); //定义二维向量
for (int i = ; i < numRows; i++)
ret[i].resize(lie1); //得到 numRows*lie1的矩阵
int i = ;
int j = ;
int i0 = ;
for (int count = ; (count <= (numRows*lie1-)) && (i0<h2); count++)
{
if (i == )
{
//如果行数等于列数就往下加
for (int ii = ; (ii < numRows) && (i0<h2) && (j<(lie1)); ii++)
{
ret[ii][j] = s[i0];
i0++;
}
i = numRows - ;
j++; }
if (i == (numRows - ))
{
for (int ii = numRows - ; (ii >= ) && (i0<h2) &&(j<(lie1)); ii--)
{
ret[ii][j] = s[i0];
j++;
i0++;
}
i = ;
}
}
//将字符串赋值给向量ret
string s1;
char *c;
for (int i1 = ; i1 < numRows; i1++)
{
for (int j1 = ; j1 < (lie1 ); j1++)
{
if (ret[i1][j1] != '\0')
{
c = &(ret[i1][j1]);
s1.append(c, ); //每次都将值赋给字符串
}
cout << ret[i1][j1];
}
cout << endl; }
return s1;
}
};
void main()
{
Solution A;
string a1 = "A";
string b;
b = A.convert(a1, );
int h1 = b.length();
cout << b<<endl;
system("pause"); }

Leetcode_实现zigzag的转换_20161228的更多相关文章

  1. SOJ--Zig-Zag

    Zig-Zag 在图形图像处理中经常须要将一个二维的图像矩阵转化为一维的向量.二维化一维的过程实际上就是将二维数组的元素按某种顺序构成一维数组. 一种经常使用的序列叫"Zig-Zag&quo ...

  2. Thrift之TProtocol系列TCompactProtocol解析

    TCompactProtocol协议作为TBinaryProtocol协议的升级强化版,都作为二进制编码传输方式,采用了一种乐器MIDI文件的编码方法(wiki,百度下),简单介绍下两种思想: 1: ...

  3. leetcode:ZigZag Conversion 曲线转换

    Question: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ...

  4. ZigZag Conversion 之字形转换字符串

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

  5. LeetCode OJ:ZigZag Conversion(字符串的Z字型转换)

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  6. [LeetCode] ZigZag Converesion 之字型转换字符串

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  7. 第6题 ZigZag转换

    题目描述如下: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ro ...

  8. [LeetCode] 6. ZigZag Conversion 之字型转换字符串

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  9. [LeetCode] 6. ZigZag Converesion 之字型转换字符串

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

随机推荐

  1. Microsoft Web Platform Installer 5.0

    Microsoft Web Platform Installer 5.0 Web 平台安装器 https://www.microsoft.com/web/downloads/platform.aspx

  2. 各种SKYPE网页代码,SKYPE在线代码

    各种SKYPE网页代码,SKYPE在线代码 <a href="skype:账户?chat" target="_blank">开始 Skype 文字聊 ...

  3. php 判断当前的操作系统的方法

    因为windows 和 linux 下的标识符是不一样的,所以写了个方法来获取当前的标识符. /** * 对操作系统进行判断 * @return string */ static function j ...

  4. phalcon: 多模块多表查找,多表sql

    那么多模块下,如何分页的,如果直接用->from(表名),报错找不到此类,此时要引用model类的全命名空间名称如下: $builder = $this->modelsManager-&g ...

  5. [经验] 新版SkyIAR、Easy Image X在有些PE里不能运行的解决办法

    [经验] 新版SkyIAR.Easy Image X在有些PE里不能运行的解决办法 xxwl2008 发表于 2013-1-26 11:58:38 https://www.itsk.com/threa ...

  6. C# 编程实现非自相交多边形质心

    计算公式公式: http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon 多边形的质心: 一个非自相交的n个顶点的多边形(x0,y0), (x ...

  7. .net预览功能

    1.把需要预览的文件格式转换为pdf 2.下载pdttoswf软件 3.实现预览swf功能 ok excel转pdf时会出现乱掉的问题.是excel文件的问题.调整excel文件的格式即可.

  8. 从下往上看--新皮层资料的读后感 第四部分 来自神经元的设计-perceptron 感知机

    搬地方了,其他的部分看知乎:https://zhuanlan.zhihu.com/p/22114481 直到50年代,perceptron被Frank Rosenblatt搞了出来.perceptro ...

  9. ARM+LINUX 项目学习总结

    一.确定功能 二.系统移植 1. 根据具体板子修改u-boot (三星的开发板资料) 2. 根据具体板子和功能修改内核 (基本的驱动) 3. 移植busybox 三.驱动修改编写 四.应用编程 附1 ...

  10. 用computed返回this.$store.state.count,store更改了,但是computed没有调用

    今天出现了这个问题,store更新了,你computed为啥不调用呢??? 另一个.vue更新了state,这个的computed就监听不到了么? 是用这种格式更新的this.$store.commi ...