#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. 1007. Maximum Subsequence Sum (25)

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...

  2. asp.net报错“尝试读取或写入受保护的内存。这通常指示其他内存已损坏”的解决办法

    来源:http://ajxfxb.blog.163.com/blog/static/56675086201411634336878/ 作者是:没完没了的工作 asp.net报错“尝试读取或写入受保护的 ...

  3. codeigniter nginx配置

    转载:http://www.nginx.cn/1134.html server{ listen 80; server_name www.ci.oa.com; access_log /usr/local ...

  4. jQuery index()

    index() index() 方法返回指定元素相对于其他指定元素的 index 位置. 语法 $(selector1).index(selector2) selector2:可选,指定元素:为空时默 ...

  5. weblogic.security.SecurityInitializationException: Authentication for user weblogic denied(详见下面具体报错信息)

    在使用nodemanager启动受管服务器时,报错 <Dec 25, 2016 5:54:31 PM CST> <Error> <NodeManager> < ...

  6. 【java基础】java的构造函数

    java构造器用于创建类的实例,是创建对象的重要途径,因此,java类必须含有一个或一个以上的构造函数   当我们没有为类提供任何构造函数的时候,系统会自动为该类提供一个无参构造函数,当我们为类提供了 ...

  7. linux工具apt、yum和dnf运用

      首先,说明一下我的环境:ubuntu16.04. 什么是APT: 高级包装工具(英语:Advanced Packaging Tools,简称:APT)是Debian及其衍生发行版(如:ubuntu ...

  8. Javascript中apply、call、bind

    网上文章虽多,大多复制粘贴,且晦涩难懂,我希望能够通过这篇文章,能够清晰的提升对apply.call.bind的认识,并且列出一些它们的妙用加深记忆. apply.call 在 javascript ...

  9. nexus离线安装索引及一些问题

    安装私服~~~ 整体流程: 服务器安装私服 配置私服(主要是安装索引) 本地maven配置指向私服 安装私服 win & linux 网上教程很多,不在阐述: 配置私服(及安装index) 1 ...

  10. 关于absolute 和 relative 定位的定义

    absolute的英文意思是绝对的意思,实际上是针对父级元素元素定位,如果父级元素没有position:relative|absolute,则追至再上一个父级元素,直至相对于文档的左上角定位,按照我们 ...