#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. jQuery事件绑定.on()简要概述及应用

    原文地址:http://www.jquerycn.cn/a_5346     前几天看到事件委托的时候,关于live()方法讲的不是很详细,就去搜了一下关于live()和delegate()的,最后看 ...

  2. js 定义方法的集中方式

    1:调用关键字function来构造 如:    function  distance(x1,x2,y1,y2)    {    var  dx = x2 - x1;    var  dy = y2  ...

  3. CentOS 6.3 安装过程

    1.放入光盘 2.安装欢迎界面 进入安装欢迎界面,有四个选项: 1.“Install or upgrade an existing system”:安装或升级现有系统 2. “Install syst ...

  4. sublime text2 换主题

    网上看到一款喜欢的主题,换掉初始的主题,感觉颜色浅一点会比较好看,看中了下面这款. 后来看到有在 github 上的人做的 ,这是地址 ,上面也有详细的如何更改的步骤,下面是我自己这里做个备份. 1. ...

  5. AJAX第二发

    开始总结自己在项目上的使用AJAX的情况: 该图为向后台请求的数据,前台返回的结果,返回的数据类型为object,businessObj由许多key组成,我们项目前台需要调用的数据就藏在数组recor ...

  6. Android TabWidget底部显示

    TabHost控件默认使用LinearLayout包裹TabWidget和FrameLayout,布局文件如下: <TabHost xmlns:android="http://sche ...

  7. node.js + webstorm :配置开发环境

    一.配置开发环境: 1.先安装node (1).访问http://nodejs.org打开安装包,正常安装,点击next即可. 为了测试是否安装成功,打开命令提示符,输入node,则进入node.js ...

  8. 收集C#常用类:产生一个验证码,改了下

    using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D ...

  9. python解析robot framework的output.xml,并生成html

    一.背景 Jenkins自动构建RF脚本,生成的RF特有HTML报告不能正常打开. 需求:用Python解析测试报告的xml数据,放在普通HTML文件中打开 二.output.xml数据 三.用pyh ...

  10. github改local用户名和email

    github改local用户名和email 进入cd ~/.ssh 修改git config --global user.name “用户名” config --global user.email 电 ...