#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. OO的五大原则是指SRP、OCP、LSP、DIP、ISP。

    OO的高层原则,面向对象设计的基本原则 设计模式之六大原则--开闭原则(OCP) 设计模式之六大原则--迪米特法则(LoD,LKP) 设计模式之六大原则--接口隔离原则(ISP) 设计模式之六大原则- ...

  2. position之fixed固定定位、absolute绝对定位和relative相对定位

    什么是层模型? 什么是层布局模型?层布局模型就像是图像软件PhotoShop中非常流行的图层编辑功能一样,每个图层能够精确定位操作,但在网页设计领域,由于网页大小的活动性,层布局没能受到热捧.但是在网 ...

  3. mysql授权

    grant all privileges on testdb to userA@'%' 然而通过userA登录查看存储过程时仍然无权限查看,通过jdbc连接程序也报错, 之前mysql在windows ...

  4. Security » Authorization » 基于视图的授权

    View Based Authorization¶ 基于视图的授权 44 of 46 people found this helpful Often a developer will want to ...

  5. SQL表关联赋值、系统表、表数据删除

    1. 表与表的关联赋值(用于表与表之间有关联字段,数据互传) 双表关联赋值 UPDATE #B SET #B.D=#A.B from #B inner join #A on #B.C=#A.A 多表关 ...

  6. You Don't Know JS: Scope & Closures(翻译)

    Chapter 1: What is Scope? 第一章:什么是作用域 One of the most fundamental paradigms of nearly all programming ...

  7. matplotlib总结

    主要使用matplot.pyplot import matplot.pyplot as plt plt.figure(1) plt.subplot(211) plt.plot(x,y, 'rs') p ...

  8. No package mysql-server available.

    Centos 7 comes with MariaDB instead of MySQL. MariaDb is a open source equivalent to MySQL and can b ...

  9. 用margin还是padding

    用margin还是用padding这个问题是每个学习CSS进阶时的必经之路. CSS边距属性定义元素周围的空间.通过使用单独的属性,可以对上.右.下.左的外边距进行设置.也可以使用简写的外边距属性同时 ...

  10. 关于Cookies与Session系列一

    这两个东西,最近项目操作的比较少,不过这两个在Web项目开发中一直都扮演着很重要的角色,有时有些细节会不小心就遗忘掉. Cookies  的概述 Cookies是由服务器端生成,发送给客户端,用来保存 ...