Leetcode_实现zigzag的转换_20161228
#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的更多相关文章
- SOJ--Zig-Zag
Zig-Zag 在图形图像处理中经常须要将一个二维的图像矩阵转化为一维的向量.二维化一维的过程实际上就是将二维数组的元素按某种顺序构成一维数组. 一种经常使用的序列叫"Zig-Zag&quo ...
- Thrift之TProtocol系列TCompactProtocol解析
TCompactProtocol协议作为TBinaryProtocol协议的升级强化版,都作为二进制编码传输方式,采用了一种乐器MIDI文件的编码方法(wiki,百度下),简单介绍下两种思想: 1: ...
- leetcode:ZigZag Conversion 曲线转换
Question: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ...
- ZigZag Conversion 之字形转换字符串
1.题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...
- LeetCode OJ:ZigZag Conversion(字符串的Z字型转换)
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- [LeetCode] ZigZag Converesion 之字型转换字符串
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 第6题 ZigZag转换
题目描述如下: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ro ...
- [LeetCode] 6. ZigZag Conversion 之字型转换字符串
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- [LeetCode] 6. ZigZag Converesion 之字型转换字符串
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
随机推荐
- Git命令行(转用于学习和记录)
Git命令行介绍和使用说明(持续更新) 参见:<Git 中文简体教程> 一. 命令“git”或者“git help”查询常用命令 [add]: “git add”——不但是用来添加不在版本 ...
- Linux下的系统调用
张雨梅 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-10000 1.linux的的用户态与内核 ...
- web开发必备插件
文本编辑器百度ueditor:http://ueditor.baidu.com/website/
- fullpage 单屏高度超过屏幕高度,实现单屏内可以滚动并解决手机端单屏高度不正确的问题
最近接触了好几次jquery.fullpage.js这个插件,实现整屏的滑动,效果很炫,用fullpage来实现也很简单,但是也碰到了一些问题和大家分享一下 1.单屏高度超过屏幕高度,实现单屏的滑动 ...
- File system needs to be upgraded. You have version null and I want version 7
安装hbase时候报错: File system needs to be upgraded. You have version null and I want version 7 注: 我安装的hba ...
- centos各版本信息
CentOS version Architectures RHEL base Kernel CentOS release date RHEL release date Delay (days) 2.1 ...
- LD_LIBRARY_PATH的设定
LD_LIBRARY_PATH的设定 变量LD_LIBRARY_PATH 是用来在Linux下设置动态链接库(*.so)的查找路径,我们一般情况下都需要在运行一个带有动态链接库的程序是运行 exp ...
- Android和iOS常用命令学习(真机)
1. 安装应用: Android: adb install xxx.apk iOS: ideviceinstaller -i xxx.ipa 2. 卸载应用 Android: abd uninstal ...
- 小tip:关于typeof,instanceof,toString(),valueOf(),toLocaleString(),join(),reverse(),sort(),pop(),push(),shift(),unshift()
typeof:用于检测一个变量是否是基本数据类型.instanceof用于检测某引用对象是什么类型的对象. var s = "Nicho"; var b = true; var n ...
- Python’s SQLAlchemy vs Other ORMs[转发 0]
原文地址:http://pythoncentral.io/sqlalchemy-vs-orms/ Overview of Python ORMs As a wonderful language, Py ...