leetcode题解 6.ZigZag Conversion
6.ZigZag Conversion 题目:
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".
这个题目首先要理解什么是zigzag。
其实就是给你一个字符串和一个数字(行数),先分析一个简单的例子
比如:convert("123456789",4)
1 7
2 6 8
3 5 9
4
就应该返回“172683594”,其实就是先给你一个字符串,然后按z字型放到一个给定的行数里。
这个题是看了一些网上的思路来完成的。
思路:
首先要有一个特殊情况就是给的行数非常少为1,那么就只能放在这一个行里面,其实这样输入
和输出的就是相同的字符串,直接返回即可,或者是行数非常之多,超过了字符串中的字符数量,
那么也很简单,就是直接返回这个字符串即可(相当于他一行放一个,一直换行)。
现在开始考虑正常的情况:外面套一个for循环,把给的字符串中的字符每个都轮流来一遍。
里面其实就是一开始行数为0,只要行数没有到规定的最多行数,行数就一直++,我是用一个
down作为一个标志。一旦到了最多行数,就把开始--,行数再到达0的时候又开始++每次循环的
时候,就把这个字符放到那个行数对应的字符串里面。最后把所有的字符串都连接起来,作为
result即可。
代码如下:
class Solution {
public:
string convert(string s, int numRows) {
string* rowStr=new string[numRows];
int numOfChar=s.length();
int begin=;
if(numRows<=||numRows>=numOfChar)
return s;
int row=;
bool down=true;
for(int i=;i<numOfChar;i++)
{
rowStr[row]=rowStr[row]+s[i];
if(row==numRows-)
down=false;
if(row==)
down=true;
if(down)
row++;
else row--;
}
string result;
for(int i=;i<numRows;i++)
result+=rowStr[i];
return result;
}
};
leetcode题解 6.ZigZag Conversion的更多相关文章
- 《LeetBook》leetcode题解(6): ZigZag Conversion[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【LeetCode】6. ZigZag Conversion Z 字形变换
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字形变换,ZigZag,题解,Leetcode, 力扣,P ...
- 【一天一道LeetCode】#6 ZigZag Conversion
一天一道LeetCode系列 (一)题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given ...
- 【LeetCode】006. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【LeetCode】6 - ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- leetcode problem 6 ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- LeetCode OJ:ZigZag Conversion(字符串的Z字型转换)
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- LeetCode(6) - ZigZag Conversion
这个题的要求是给你一个字符串,和一个行数,例如(s = "mysisteristhemostlovelygirl" , row = 4),每一行一个字符串,但是s却得按照zigza ...
- 【LeetCode】6. ZigZag Conversion 锯齿形转换
题目: 思路: 以图为例:s={'A','B','C','D','E','F','G','H'.....} 1.先不考虑中间元素F.G.H.N...,每一行前后元素在数组中对应下标相差size=2*n ...
随机推荐
- Css Secret 案例Demo全套
Css Secret 案例全套 github地址 案例地址 去年买了一本CSS揭秘的css专题书,该书揭示了 47 个鲜为人知的 CSS 技巧,主要内容包括背景与边框.形状. 视觉效果.字体排印.用户 ...
- 无后台应用 Stash Backend
Stash Backend 是Github上的开源项目 https://github.com/gaboratorium/stash,目的在于提供一套方便使用.方便部署的后台应用.特别适合为Web前端和 ...
- MySQL Connector 卸载
MySQL Connector 安装的时候有时候会遇到很多问题,有时候会卸载失败,导致无法重新安装.测试了网上各种办法,删文件,删注册表,重启,360强行删除都不是很有效.最后发现msizap比较有效 ...
- ubuntu,kali linux和windows三系统流水账——写给自己
我先说一下ubuntu和windows双系统安装的几种方法,最后总结kali linux的安装,想起什么写什么,所以有点乱.然后记录一下自己的使用过程中遇见的问题和解决的方法,还有我的个人建议. 我个 ...
- 可能是最好的SQL入门教程
个人博客:这可能是最好的SQL入门教程
- python函数式编程之生成器
在前面的学习过程中,我们知道,迭代器有两个好处: 一是不依赖索引的统一的迭代方法 二是惰性计算,节省内存 但是迭代器也有自己的显著的缺点,那就是 不如按照索引取值方便 一次性,只能向后取值,不能向前取 ...
- Vue自定义插件方法大全
新年第一天首先祝大家新年快乐,心想事成! 1.利用根实例构造函数的原型 //在构造函数的原型链上添加自定义属性 Vue.prototype.test = 'pomelo' //在其他组件中调用 con ...
- [模拟赛] T1 高级打字机
Description 早苗入手了最新的高级打字机.最新款自然有着与以往不同的功能,那就是它具备撤销功能,厉害吧. 请为这种高级打字机设计一个程序,支持如下3种操作: 1.T x:在文章末尾打下一个小 ...
- python列表操作符
list1=[123,456] list2=[234,234] list1>list2 >>>False#返回False 第一项比较之后直接返回false,第二项不看 #+实现 ...
- java.lang.Object学习总结