【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 ...
随机推荐
- ABI and compiler
http://stackoverflow.com/questions/2171177/what-is-application-binary-interface-abi ABIs cover detai ...
- PHP 5.4 on CentOS/RHEL 7.0, 6.5 and 5.10 via Yum
PHP 5.4.36 has been released on PHP.net on 18th December 2014, and is also available for CentOS/RHEL ...
- 问题 B: 投简历
题目描述 小华历经12寒窗苦读,又经历4年大学磨砺,终于毕业了,随着毕业季的到来,找工作也日益紧张起来.由于要面试不同的公司,因此小华需要准备不同的简历.当然最基本的信息是必不可少的,基本信息:姓名. ...
- Docker中的三个基本概念容器(container)、镜像(image)和仓库(registry)之间有什么关系?
Docker镜像是一个特殊的文件系统,除了提供容器运行时所需的程序.库.资源.配置等文件外,还包含了一些为运行时准备的一些配置参数(如匿名卷.环境变量.用户等).镜像不包含任何动态数据,其内容在构建之 ...
- python doctest测试
title: Python doctest测试 tags: Python --- doctest测试 python 提供了REPL(read-eval-print loop,读取.求值.输出的循环) ...
- failed to bind pixmap to texture
问题描述:我用的是Ubuntue的操作系统,终端突然挂了.我重启了一下电脑,就进不去系统了. 日志信息: failed to bind pixmap to texture 原因: 界面管理工具坏了, ...
- C#语言概述
C#语言概述 一..NET Framework .NET Framework是Windows的一个不可或缺的组件,它包括公共语言运行库(CLR)和类库两部分. CLR是Microsoft的公共语言基础 ...
- django中介模型,CBV模型,及logging日志配制
1.中介模型 中介模型,这个是在我们创建表格时,多对多添加的时候应用到的,通过制定ManyToManyField字段中的through参数来定义,为两者的关系新建一个中介class 为什么会产生这个中 ...
- 用纯css改变默认的radio和checkbox的样式
利用css的label的伪类(::before)代替checkbox和radio效果: 优点:需要图片来调整选中前和选中后的样式,纯css搞定 缺点:兼容性,IE8以下不支持 在线例子: css改变默 ...
- 3、SpringBoot+Mybatis整合------主键回填
开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis01/tree/d68efe51774fc4d96e5c6870 ...