LeetCode 06 ZigZag Conversion
https://leetcode.com/problems/zigzag-conversion/
水题纯考细心
题目:依照Z字形来把一个字符串写成矩阵,然后逐行输出矩阵。
O(n)能够处理掉
记i为行数
第0行和第numRow-1行。 ans += str[i+k*(numRows*2-2)], k=0,1,2,...
其它, 每一个Z字形(事实上仅仅是一竖一斜两条线)须要加上两个,下标见代码。
特殊处理:numRows=1的时候numRows*2-2==0 ,会死循环的。另外numRows=1的时候直接输出即可
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std; class Solution {
public:
string convert(string s, int numRows) {
string ret;
if(numRows == 1){
return s;
}
for(int i=0; i<numRows; i++){
if(i == 0 || i == numRows-1){
int j=i;
while(j<s.size()){
ret = ret + s[j];
j += numRows*2-2;
}
}else{
int j=i;
while(j<s.size()){
ret = ret + s[j];
if(j+numRows*2-2-(i*2) < s.size())ret = ret + s[j+numRows*2-2-(i*2)];
j += numRows*2-2;
}
} }
return ret;
}
}; int main(){
string s;
int numRows;
Solution sol;
while(cin >> s >> numRows){
cout << sol.convert(s, numRows) << endl;
}
return 0;
}
LeetCode 06 ZigZag Conversion的更多相关文章
- LeetCode 6. ZigZag Conversion & 字符串
ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...
- Leetcode 6. ZigZag Conversion(找规律,水题)
6. ZigZag Conversion Medium The string "PAYPALISHIRING" is written in a zigzag pattern on ...
- LeetCode 6 ZigZag Conversion 模拟 难度:0
https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in ...
- LeetCode 6 ZigZag Conversion(规律)
题目来源:https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is writt ...
- [LeetCode][Python]ZigZag Conversion
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/zigzag- ...
- 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]
题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- [LeetCode 题解]: ZigZag Conversion
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 The string ...
- [LeetCode] 6. ZigZag Conversion 之字型转换字符串
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【leetcode】ZigZag Conversion
题目简述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...
随机推荐
- Git 如何把master的内容更新到分支
Background: 当有人对master进行更新之后,你想让已经创建的分支内容更新到master的最新状态, bpan@5CG7022BM2 MINGW64 /d/GitRep/JIRA_Exte ...
- linu基础入门(一)
原创作品,允许转载,转载时请务必声明作者信息和本声明. https://www.cnblogs.com/zhu520/p/10730550.html 本人小白,有错指出.谢谢! 一:根据上一步安装与新 ...
- C++里面关于虚函数的一些注意点
最后,总结一下关于虚函数的一些常见问题: 1) 虚函数是动态绑定的,也就是说,使用虚函数的指针和引用能够正确找到实际类的对应函数,而不是执行定义类的函数.这是虚函数的基本功能,就不再解释了. 2) 构 ...
- awk条件语句
条件语句用于在运行操作之前做一个測试.在前面的章节中,我们看到了模式匹配规则的一些演示样例. 模式匹配规则本质上就是影响输入循环的条件表达式. 在这一部分,我们主要就在action中所使用的条件语句进 ...
- java枚举在android项目应用
今天修复一个公司非常早曾经的android应用功能,里面的代码逻辑已经全然错乱,然后发现返回的数据全然不正确了.然后修复了整整两天.然后我又一次整理了一遍,重构就算不上了. 然后就用上了枚举. 什么是 ...
- 会变得ActionBar,让你的ActionBar与众不同
话不多说先看两张图: github地址:https://github.com/Smalinuxer/android-SlideActionBar 原理什么的有时间再讲,或者自行看代码; 兴许还会补充新 ...
- Light oj 1138 - Trailing Zeroes (III) 【二分查找好题】【 给出N!末尾有连续的Q个0,让你求最小的N】
1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...
- CF Mike and Feet (求连续区间内长度为i的最小值)单调栈
Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- vue25---vue2.0变化
组件模版: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- vue11 vue实例方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...