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 ...
随机推荐
- 对比学习:《深度学习之Pytorch》《PyTorch深度学习实战》+代码
PyTorch是一个基于Python的深度学习平台,该平台简单易用上手快,从计算机视觉.自然语言处理再到强化学习,PyTorch的功能强大,支持PyTorch的工具包有用于自然语言处理的Allen N ...
- 紫书 习题 10-4 UVa 1644(素数筛)
素数筛没什么好说的 #include<cstdio> #include<vector> #include<cstring> #define REP(i, a, b) ...
- ImageLoader的简单分析(二)
在<ImageLoader的简单分析>这篇博客中对IImageLoader三大组件的创建过程以及三者之间的关系做了说明.同一时候文章的最后也简单的说明了一下ImageLoader是怎么通过 ...
- [Python] Histograms for analysis Daily return
A histogram is an accurate representation of the distribution of numerical data. Y axis is the occur ...
- 网络流 HDU 3549 Flow Problem
网络流 HDU 3549 Flow Problem 题目:pid=3549">http://acm.hdu.edu.cn/showproblem.php?pid=3549 用增广路算法 ...
- MyEclipse连接不上genymotion的解决方式
奇怪的是我的MyEclipse有时候连接得上genymotion,有时候又连接不上.之前连接不上的时候,就直接用真机调试,因此出现这个问题非常久了一直都没有去找解决方式.今天认真的反省了自己,再也不能 ...
- thinkphp中cookie和session中操作数组的方法
thinkphp中cookie和session中操作数组的方法 一.ThinkPHP模板中如何操作session,以及如果session中保存的是数组的情况 在ThinkPHP的模板中操作sessio ...
- 父子间通信四 ($dispatch 和 $broadcast用法)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Juniper Alarms 灯红色报警处理
1.2.5.1告警查看 root# run show system alarms 2 alarms currently active Alarm time Class Description 2015 ...
- vue+ webpack中的animate.css实现的执行多个连续的动画
1.安装 npm install animate.css 2.使用方法 入口文件App中进行引入 import animate from 'animate.css' 3.多个连续的动画 实现的效果:实 ...