【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 ...
随机推荐
- 智能开关:orange pi one(arm linux)控制继电器
大家都知道,继电器是用小电流去控制大电流运作的一种“自动开关”,在我们生活.工作中随处可见.现在的“智能家居”概念,有很多功能模块其实就是“智能开关”,远程开关.定时开关.条件触发开关等等. 下面介绍 ...
- ie6下按钮下边框消失不显示的问题
最近网站做改版,又发现一个ie6奇葩的问题,就一个很普通带边框的按钮,但在ie6中下边框不显示,ie7没有测试不知道是不是也不显示,其他浏览器正常 代码和预览效果如下: <style> b ...
- 页面文本超出后CSS实现隐藏的方法
text-overflow: ellipsis !important; white-space: nowrap !important; overflow: hidden !important; dis ...
- POJ-3274 Gold Balanced Lineup---hash经典题!
题目链接: https://vjudge.net/problem/POJ-3274 题目大意: 给定多头牛的属性,每头牛的属性由一个非负数表示,该数的二进制表示不会超过K位,它的二进制表示的每一位若为 ...
- 字符串查找算法的改进-hash查找算法
字符串查找即为特征查找: 特征即位hash: 1.将待查找的字符串hash: 2.在容器字符串中找头字符匹配的字符串,并进行hash: 3.比较hash的结果:相同即位匹配: hash算法的设计为其中 ...
- Poj(1789),最小生成树,Prim
题目链接:http://poj.org/problem?id=1789 还是套路. #include <stdio.h> #include <string.h> #define ...
- oracle: listener.ora 、sqlnet.ora 、tnsnames.ora的配置及例子
1.解决问题:TNS或者数据库不能登录. 最简单有效方法:使用oracle系统提供的工具 netca 配置(把原来的删除掉重新配置) $netca 2.然而,仍有疑问:如何指定'l ...
- ckplayer插件播放视频
官网:超酷网页视频 本篇介绍:插件播放视频, 添加插件(额外的功能), 视频格式之间的转换 PC端一般都是利用插件(Flash)来播放视频,可以支持很多格式.网上很多说这个插件的,这里只是做个记录 ...
- C编程经验总结5(剧终)
Prolog是AI编程语言 Git(分布式) svn(集中式) commit(委托,犯罪) patch补丁) merge(融入) repository(仓库) Ctrl+s(保存) r ...
- 如何把设计图自动转换为iOS代码? 在线等,挺急的!
这是一篇可能略显枯燥的技术深度讨论与实践文章.如何把设计图自动转换为对应的iOS代码?作为一个 iOS开发爱好者,这是我很感兴趣的一个话题.最近也确实有了些许灵感,也确实取得了一点小成果,和大家分享一 ...