题目:

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".

代码:

class Solution {
public:
string convert(string s, int numRows) {
const int len = s.size();
if ( len<numRows || numRows== ) return s;
vector<char> ret;
const int INTERVAL = *numRows-;
for ( int i=; i<numRows; ++i )
{
int interval = *(numRows-i)-;
for ( int j=i; j<len; interval=INTERVAL-interval)
{
ret.push_back(s[j]);
j = (interval==INTERVAL||interval==) ? j+INTERVAL : j+interval;
}
}
return string(ret.begin(),ret.end());
}
};

tips:

找到ZigZag每行元素在元字符串s中间隔大小的规律。

=======================================

第二次过这道题,思路还是第一次的思路,但是代码不如第一次简洁了。

class Solution {
public:
string convert(string s, int numRows) {
vector<char> ret;
if ( numRows== ) return s;
const int interval = *(numRows-);
for ( int i=; i<numRows; ++i )
{
int j = i;
int preInterval = *i;
while ( j<s.size() )
{
ret.push_back(s[j]);
if ( preInterval!= && preInterval!=interval )
{
j = j + interval - preInterval;
preInterval = interval - preInterval;
}
else
{
j = j + interval;
}
}
}
return string(ret.begin(),ret.end());
}
};

【ZigZag Conversion】cpp的更多相关文章

  1. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  2. Hdu 4734 【数位DP】.cpp

    题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...

  3. 【Valid Sudoku】cpp

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  4. 【Permutations II】cpp

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  5. 【Subsets II】cpp

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  6. 【Sort Colors】cpp

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  7. 【Sort List】cpp

    题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...

  8. 【Path Sum】cpp

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  9. 【Symmetric Tree】cpp

    题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...

随机推荐

  1. python 字符串部分总结

    字符串 对于单个字符的编码,Python提供了ord()函数获取字符的整数表示,chr()函数把编码转换为对应的字符 >>> ord('A') 65 >>> ord ...

  2. jade简介

    模板引擎:将动静部分糅合的一种实现机制或者技术 var items = [ {title:'..',photo:'http://',id:1,desc:'a'}, {title:'..',photo: ...

  3. python导入其他文件夹下的.py文件

    想在globalpararm中导入read_config中的类 import sys sys.path.append('..') from common.read_config import Read ...

  4. Hubtown(最大流)

    Hubtown 时间限制: 1 Sec  内存限制: 128 MB提交: 23  解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 Hubtown is a large N ...

  5. 20145238-荆玉茗 《Java程序设计》第7周学习总结

    20145238 <Java程序设计>第7周学习总结 教材学习内容总结 第13章时间与日期 13.1.1 ·即使标注为GMT(格林威治时间),实际上谈到的的是UTC(Unix时间)时间. ...

  6. centos 6.5 配置nginx环境

    1.卸载系统中默认的php和httpd [root@x ~]# yum remove httpd* php* Loaded plugins: fastestmirror Setting up Remo ...

  7. left join后面加上where条件浅析

    select a.*,b.* from table1 a left join table2 b on b.X=a.X where XXX 如上:一旦使用了left join,没有where条件时,左表 ...

  8. AngularJS 表达式中添加过滤器实例

    过滤器可以通过一个管道字符(|)和一个过滤器添加到表达式中 历练实例: <!DOCTYPE html><html><head><meta http-equiv ...

  9. JavaScript---ECMA对象

    1.对象的概念及分类 1.1 ECMAScript中没有类,但定义了“对象”,逻辑上等价于其他程序设计语言中的类. var o = new Object(); 1.2 本地对象(native obje ...

  10. Dapper and Repository Pattern in MVC

    大家好,首先原谅我标题是英文的,因为我想不出好的中文标题. 这里我个人写了一个Dapper.net 的Repository模式的底层基础框架. 涉及内容: Dapper.net结合Repository ...