[LeetCode]题解(python):006-ZigZag Conversion
题目来源:
https://leetcode.com/problems/zigzag-conversion/
题意分析:
这道题目是字符串处理的题目。输入一个字符串和一个数字,将字符串填入倒Z形输入字符串,然后按照列读取字符,得到一个新的字符,输出这个字符。例如:字符串"PAYPALISHIRING",3
| P | A | H | N | |||
| A | P | L | S | I | I | G |
| Y | I | R |
得到的新字符是”PAHNAPLSIIGYIR”。
题目思路:
这题只是简单的字符串处理。首先,我们可以对Z形字符串进行简单优化一下,将Z中的“折”合起来。比如:“ABCDEFGHIJKL”,4
| A | G | ||||
| B | F | H | L | ||
| C | E | I | K | ||
| D | J |
优化后:
| A | G | ||
| B | F | H | L |
| C | E | I | K |
| D | J |
不难发现,得到的结果不变,而列表的列数减少了。在C和C++中可以直接定义一个二维数组,将字符读入,然后直接读取即可。但是用python初始化列表比较麻烦,我觉得直接通过下标规律可以直接得到,第一行和最后一行下一个下标是上一个+ 2*numRows – 2;而其他是先+ 2*(numRows - i) – 2 再+ 2*i,i是第几行。
代码(python):
class Solution(object):
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
size = len(s)
if size <= numRows or numRows == 1:
return s
ans = ''
i = 0
while i < numRows:
j = i
if i == 0 or i == numRows - 1:
while j < size:
ans += s[j]
j += 2*numRows - 2
if 2 * numRows - 2 == 0:
break
else:
while j < size:
ans += s[j]
j += 2*(numRows - i) - 2
if j >= size:
break
ans += s[j]
j += 2*i
i += 1
return ans
转载请注明出处:http://www.cnblogs.com/chruny/p/4798575.html
[LeetCode]题解(python):006-ZigZag Conversion的更多相关文章
- 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--No.006 ZigZag Conversion
6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...
- 【LeetCode】006. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【JAVA、C++】LeetCode 006 ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- [Leetcode]006. ZigZag Conversion
public class Solution { public String convert(String s, int nRows) { if (s == null || s.isEmpty() || ...
- 006 ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- leetcode第六题--ZigZag Conversion
Problem: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of r ...
- LeetCode之“字符串”:ZigZag Conversion
题目链接 题目要求: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ...
- LeetCode(6) ZigZag Conversion
题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
随机推荐
- JS isArray记录
var isArray=Function.isArray||function(0){ return typeof o === "object" && Object. ...
- 教你爱上Blocks(闭包)
传值 Blocks是C语言的扩充功能:带有自动变量(局部变量)的匿名函数.通过Blocks,源代码中就能使用匿名函数,即不带名称的函数.在我们 的工作中,命名占据了很大一部分,函数名,变量名,属性名, ...
- socketio 握手前中断报错
前两天折腾了下socketio,部署完发现通过nginx代理之后前端的socket无法和后端通信了,于是暴查一通,最后解决问题: location / { proxy_pass http://127. ...
- matlab GUI之自定义菜单小结
自定义菜单 1.uimenu对象 h=uimenu('PropertyName','ProperValue') h=uimenu(parent,'PropertyName','ProperValue' ...
- decimal类型不能为空,自定义update更新null值的问题。
if (!string.IsNullOrEmpty(yt_time_limit_1)) { entity["yt_time_limit_1"] = Convert.ToDecima ...
- button变成href (即按钮超链效果)
法一: 这种方法适合做单纯的HTML静态页面,因为它只有button的显示效果,但不能真的跳转.貌似是鸡肋,没多大用. 法二: 1.新打开一个页面 2.本页打开 在超链中实现打开新页面用targe ...
- Spring MVC返回的json如何去除根节点名称
spring xml中配置视图如果是如下 <property name="defaultViews"> <list> <bean class=&quo ...
- MQ学习(二)----ActiveMQ简介(转)
1. 什么是ActiveMQ ActiveMQ是一种开源的,实现了JMS1.1规范的,面向消息(MOM)的中间件,为应用程序提供高效的.可扩展的.稳定的和安全的企业级消息通信.ActiveMQ使用A ...
- C++小知识之Map用法
Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作! 1. map最基本的构造函数: map<st ...
- grunt打包过程中的注意点
1.安装nodeJS nodeJS下载地址: http://www.nodejs.org/download/ 2. 在Node.js command prompt 这个控制面板输入 npm i ...