【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 ...
随机推荐
- BZOJ 4679/Hdu5331 Simple Problem LCT or 树链剖分
4679: Hdu5331 Simple Problem 题意: 考场上,看到这道题就让我想起BZOJ4712洪水.然后思路就被带着飞起了,完全没去考虑一条链的情况,于是GG. 解法:先考虑一条链的做 ...
- 数集合有多少个TOJ(2469)
题目链接:http://acm.tju.edu.cn/toj/showp2469.html 感觉这个题目有点问题,算了不管他了,反正A了. 这里要注意的是求这个集合有多少种,那么就是要剔除重复数后,再 ...
- vuejs样式绑定
第一种:class的对象绑定,class引用的是一个对象,这个对象的属性显示不显示由变量决定 <style> .activated{ color:red; } </style> ...
- maven没有servlet(创建servlet后报错)
maven不能创建servlet 解决方案 方案一 在项目的iml进行指定根目录 <sourceRoots> <root url="file://$MODULE_DIR$/ ...
- 散度(Divergence)和旋度(Curl)
原文链接 散度(Divergence) 散度的讨论应从向量和向量场说起.向量是数学中研究多维计算的基本概念.比如,速度可以分解为相互独立的分量,则速度就是一个多维的向量.假如空间中的每一个位置都有一个 ...
- C#定义常量的两种方法
在C#中定义常量的方式有两种,一种叫做静态常量(Compile-time constant),另一种叫做动态常量(Runtime constant).前者用“const”来定义,后者用“readonl ...
- js、jquery初始化加载顺序
// ready 这个方法只是在页面所有的DOM加载完毕后就会触发 // 方式1 $(function(){ // do something }); // 方式2 $(document).ready( ...
- expect配合shell 实现自动分发秘钥文件
expect使用场景 有时候需要批量地执行一些操作,或者执行自动化的操作的时候,有些指令需要交互式地进行这就会有很多麻烦,linux下有一个程序交expect,它可以模拟键盘输入文本,省去人工干预交互 ...
- oracle时间计算
1.在给定时间上加减天数 SQL> select to_char(to_date('20170531000000','yyyymmdd HH24:MI:SS')+4,'YYYYMMDDHH24M ...
- 图解HTTP总结(6)——HTTP首部
HTTP报文首部 HTTP 协议的请求和响应报文中必定包含 HTTP 首部. 首部内容为客户端和服务器分别处理请求和响应提供所需要的信息. 对于客户端用户来说, 这些信息中的大部分内容都无须亲自查看. ...