题目说明:

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

P   A   H   N
A P L S I I G
Y I R

And then read line by line: "PAHNAPLSIIGYIR"

Write the code that will take a string and make this conversion given a number of rows:

string convert(string text, int nRows);

convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".

 

程序代码:

#include <gtest/gtest.h>
using namespace std; string convert(string s, int numRows)
{
if (s.empty() || (numRows < 2))
{
return s;
} string result;
int length = s.length();
char* data = new char[length*numRows];
memset(data,0,length * numRows); int delta = 0;
int offset = 0;
int flag = 1; for (int i= 0; i<length; ++i)
{
data[offset + delta * length] = s[i];
if ( (delta+flag) >= numRows)
{
flag = -1;
}
else if ((delta+flag) < 0)
{
flag = 1;
}
delta += flag; if (flag == -1)
{
offset ++;
}
} for (int i=0; i<numRows; ++i)
{
for (int j=0; j < length; ++j)
{
if (data[j+i*length])
result += data[j+i*length];
}
} delete data; return result;
} TEST(Pratices, tZigZagConversion)
{
// ''=> ''
// ABCDEFG 3 => A E => AEBDFCG
// B D F
// C G ASSERT_EQ(convert("",1),"");
ASSERT_EQ(convert("ABCDEFG",1),"ABCDEFG");
}

最新文章

  1. OpenCASCADE AIS Manipulator
  2. C#------接口的理解
  3. git学习——git理解和仓库的创建
  4. jquery学习笔记:获取下拉框的值和下拉框的txt
  5. DELPHI声明一个指针变量,什么时候需要分配内存,什么时候不需要分配内存?
  6. 【Leetcode】【Medium】Palindrome Partitioning
  7. android notification 传值关键
  8. h5video标签
  9. selenium自动化测试——常见的八种元素定位方法
  10. 使用img2html把图片转为网页
  11. do not track
  12. Qt 编程指南 3 信号和槽沟通
  13. China Intelligent Office Summit(2018.11.21)
  14. 20165205 2017-2018-2 《Java程序设计》 第二周学习总结
  15. 深入浅出 kvm qemu libvirt
  16. Castle DynamicProxy creation出现COMException(0x800703fa)错误的解决方案
  17. Python入门之os.walk()方法
  18. [转载]Visual Studio支持Node.js
  19. 【BZOJ】1061: [Noi2008]志愿者招募
  20. 分解大量switch-case分支的两种方法

热门文章

  1. python-tornado-hello,world
  2. Mac 安装tensorflow
  3. 什么是SharePoint?
  4. Softwaretechnik
  5. SpringMVC内置的精准数据绑定2
  6. HandlerInterceptor与MethodInterceptor
  7. 【原】jQuery easyUI 快速搭建前端框架
  8. nodejs添加jsonwebtoken验证
  9. mysql 登录报错:ERROR 1045 (28000)
  10. AJAX同步问题