【ZigZag Conversion】cpp
题目:
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的更多相关文章
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- 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~ ...
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- 【Subsets II】cpp
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 【Sort Colors】cpp
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 【Sort List】cpp
题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...
- 【Path Sum】cpp
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- 【Symmetric Tree】cpp
题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...
随机推荐
- IOS 线程的总结(及cell的图片下载)
零.线程的注意点(掌握) 1.不要同时开太多的线程(1~3条线程即可,不要超过5条)2.线程概念1> 主线程 : UI线程,显示.刷新UI界面,处理UI控件的事件2> 子线程 : 后台线程 ...
- Jenkins使用分组过滤分类
背景:Jenkins项目过多,通过选项卡的方式过滤需要的项目 1.点击选择卡上的加号 2.填写要分组的名字 3.可选择某个job进行分类,或者使用正则表达式的方式进行分类,楼主是根据正则进行匹配, 4 ...
- 一个TCP的问题,所谓TCP面向连接的虚电路到底是怎么实现的?
http://www.cskaoyan.com/thread-23715-1-1.html 今天我又查阅了一些资料,发觉一个问题,TCP所谓的面向连接的特性以及建立虚电路,与网络层虚电路服务好像有着本 ...
- 正则表达式 /i /g /m /ig /gi
正则表达式中/i,/g,/ig,/gi,/m的区别和含义 /i (忽略大小写) /g (全文查找出现的所有匹配字符) /m (多行查找) / /ig(全文查找.忽略大小写)
- 初探12C碰到的那些“坑”
一个昏天黑地的早上,刚搭建的系统忽然遭遇严重数据库问题.于是,主要人物闪亮登场了,他们分别是友商人员小灰和DBA小Y. 事情的开始,小Y接到小灰紧急救助电话... 小灰:小Y,我是友商的小灰,刚搭建的 ...
- Q&A - Apache、Nginx与Tomcat的区别?
一. 定义: 1. Apache Apache HTTP服务器是一个模块化的服务器,可以运行在几乎所有广泛使用的计算机平台上.其属于应用服务器.Apache支持支持模块多,性能稳定,A ...
- SpringMVC注解@RequestParam解析
1.可以对传入参数指定参数名 1 @RequestParam String inputStr 2 // 下面的对传入参数指定为param,如果前端不传param参数名,会报错 3 @RequestPa ...
- linux 开机自启动 Tomcat
1.修改脚本文件rc.local:vim /etc/rc.d/rc.local 这个脚本是使用者自定的开机启动程序,可以在里面添加想在系统启动之后执行的脚本或者脚本执行命令 2.添加如下内容: exp ...
- Java执行存储过程
1.JDBC调用存储过程: CallableStatement /** *p是要调用的存储过程的名字,存储过程的4个参数,用4个?号占位符代替 *其余地方写法固定 */ CallableStateme ...
- php-5.6.26源代码 - opcode执行
文件 php-5.6.26/Zend/zend_vm_execute.h ZEND_API void execute_ex(zend_execute_data *execute_data TSRMLS ...