leetcode6:Zigzag Conversion@Python
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)排列成矩形,将矩阵每一行连接起来构成一个字符串。

将矩阵压缩得到:

#-*-coding:utf-8-*- class Solution(object):
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
if numRows == 1:
return s
zigzag = ['' for i in range(numRows)] # 初始化zigzag为['','','']
row = 0 # 当前的行数
step = 1 # 步数:控制数据的输入
for c in s:
if row == 0:
step = 1
if row == numRows - 1:
step = -1
zigzag[row] += c
row += step
return ''.join(zigzag)
leetcode6:Zigzag Conversion@Python的更多相关文章
- LeetCode6 ZigZag Conversion
题意: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- [leetcode]ZigZag Conversion @ Python
原题地址:https://oj.leetcode.com/problems/zigzag-conversion/ 题意: The string "PAYPALISHIRING" i ...
- 【leetcode❤python】 6. ZigZag Conversion
#-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object): def convert(self, s, numRow ...
- 64. ZigZag Conversion
ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...
- No.006 ZigZag Conversion
6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...
- leetcode第六题 ZigZag Conversion (java)
ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...
- leetcode题解 6.ZigZag Conversion
6.ZigZag Conversion 题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a gi ...
- 6.[leetcode] ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 字符串按照Z旋转90度然后上下翻转的字形按行输出字符串--ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
随机推荐
- (转)A Survival Guide to a PhD
Andrej Karpathy blog About Hacker's guide to Neural Networks A Survival Guide to a PhD Sep 7, 2016 T ...
- solr6.0.0 + tomcat8 配置问题
中间件需求: apache-tomcat-8.0.32.tar.gz jdk-8u74-linux-x64.rpm solr-6.0.0.zip 0.安装java JDK rpm -ivh jdk-8 ...
- [Linux] - CentOS 安装nginx
linux版本:CentOS 6.0+ 安装nginx方法: 1.下载nginx rpm包命令: wget http://nginx.org/packages/centos/6/noarch/RPMS ...
- 怎么查看bios版本
怎么查看bios版本呢?无需去注册表查看,无需去BIOS中查看,只需要一条简单的命令即可,下面就来一起看一看怎么查看bios版本: Win键+R打开“运行”,然后再“运行”中输入cmd进而打开“cmd ...
- mybatis实战教程(mybatis in action)之五:与spring3集成
在这一系列文章中,前面讲到纯粹用mybatis 连接数据库,然后 进行增删改查,以及多表联合查询的的例子,但实际项目中,通常会用 spring 这个沾合剂来管理 datasource 等.充分利用sp ...
- ionic 启用sass
转入ionic项目目录,命令行下执行:ionic setup sass 提示编译器未装: You have specified Ionic CI to set up sass.However, you ...
- 黄聪:HtmlAgilityPack中SelectSingleNode的XPath和CSS选择器
XPath和CSS选择器 原文:http://ejohn.org/blog/xpath-css-selectors 最近,我做了很多工作来实现一个同时支持XPath和CSS 3的解析器,令我惊讶的是: ...
- [Mongdb] 关于Replica Set复制集奇数成员限制的解释--待完善
一.缘由: http://blog.itpub.net/29254281/viewspace-1176821/ http://blog.chinaunix.net/uid-20726500-id-54 ...
- 用python脚本通过excel生成文件夹树结构
大概这样写标题是对的吧... 目标: 通过excel目录结构文档生成文件夹树结构. 也就是: 通过下面的excel
- python中的单引号,双引号,三引号
转载自: http://blog.csdn.net/wanghai__/article/details/6285310 先说1双引号与3个双引号的区别,双引号所表示的字符串通常要写成一行 如: s1 ...