leetcode-algorithms-6 ZigZag Conversion
leetcode-algorithms-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 s, int numRows);
Example 1:
Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"
Example 2:
Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:
P I N
A L S I G
Y A H R
P I
解法
观察输出后的字符串,可得出规律:
- row = 0(第一行)时,对于index k = k * (2 * numRows - 2);
- row = numRows - 1(最后一行),对于index k = k * (2 * numRows - 2) + numRows - 1;
- row = i(第i行),有两种情况,k * (2 * numRows - 2) + i和 (k + 1) * (2 * numRows - 2) - i;
class Solution
{
public:
string convert(string s, int numRows)
{
if (numRows == 1) return s;
std::string convertString;
int n = s.size();
int loopLen = 2 * numRows - 2;
for (int i = 0; i < numRows; i++)
{
for (int j = 0; j + i < n; j += loopLen)
{
convertString += s[j + i];
if (i != 0 && i != numRows - 1 && j + loopLen - i < n)
convertString += s[j + loopLen - i];
}
}
return convertString;
}
};
时间复杂度: O(n).
空间复杂度: O(1).
leetcode-algorithms-6 ZigZag Conversion的更多相关文章
- leetcode题解 6.ZigZag Conversion
6.ZigZag Conversion 题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a gi ...
- 《LeetBook》leetcode题解(6): ZigZag Conversion[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【一天一道LeetCode】#6 ZigZag Conversion
一天一道LeetCode系列 (一)题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given ...
- 【LeetCode】6. ZigZag Conversion Z 字形变换
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字形变换,ZigZag,题解,Leetcode, 力扣,P ...
- 【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】006. ZigZag Conversion
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 ...
随机推荐
- Jenkins简介
Jenkins 是一个开源项目,提供了一种易于使用的持续集成系统,使开发者从繁杂的集成中解脱出来,专注于更为重要的业务逻辑实现上.同时 Jenkins 能实施监控集成中存在的错误,提供详细的日志文件和 ...
- js转义和反转义html
本文地址: http://www.cnblogs.com/daysme/p/7100553.html 下面的代码网上常用有,但不是想要的. JS实现HTML标签转义及反转义 http://blog.c ...
- js字符串与十六进制相互转换
1.字符串(汉字)转换为十六进制 主要使用字符串.charCodeAt()方法,此方法返回一个字符的Unicode值,再用toString(16)方法,该方法是先将数字对象转换为二进制,再把二进制转化 ...
- HDU 4403 A very hard Aoshu problem(dfs爆搜)
http://acm.hdu.edu.cn/showproblem.php?pid=4403 题意: 给出一串数字,在里面添加一个等号和多个+号,使得等式成立,问有多少种不同的式子. 思路: 数据量比 ...
- 使用MVCPager做AJAX分页所需要注意的地方
1.版本问题,推荐使用2.0以上,对ajax支持才比较的好了 2.当需要使用页索引输入或下拉框以及使用Ajax分页模式时,必须用Html.RegisterMvcPagerScriptResource方 ...
- mark mem
韦达定理 http://baike.baidu.com/link?url=M45ozZEnQ4BtKD7l22WWgQuGnmDYV7TFynQcPEO2Tt8leYGhyEa1flt-RM34NG4 ...
- JavaSE习题 第四章 类与对象
问答题: 1.在声明类时,类名应该遵守哪些习惯? 1.与文件名相同2.首字母大写 2.类体内容中有那两类比较重要的成员? 1.成员变量2.方法 3.实例方法可以操作类变量吗?类方法可以操作实例变量吗? ...
- Oracle其他简单查询
范例:查询公司中所有雇员的职位信息 SELECT job FROM emp; 实际在公司里面,一个职位会有多个人员.如果查询全部职位,肯定会存在重复.要消除掉重复,利用DISTINCT完成.(dist ...
- linux 打开一个文件现swap文件
转自:http://blog.csdn.net/eckelwei/article/details/17078187 有时候在用vim打开文件时提示类似以下的信息: 发现交换文件 ".expo ...
- Rxbus的使用
Rxbus是一种模式,在RxJava中 一.添加依赖 compile 'io.reactivex:rxandroid:1.2.0' compile 'io.reactivex:rxjava:1.1.5 ...