[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 ...
随机推荐
- 设置ToggleButton、Switch、CheckBox和RadioButton的显示效果
ToggleButton.Switch.CheckBox和RadioButton都是继承自android.widget.CompoundButton,意思是可选择的,因此它们的用法都很类似.Compo ...
- Windows7中搭建Android x86_64及armv8-a操作步骤
1. 从https://developer.android.com/tools/sdk/ndk/index.html 下载android-ndk-r10d-windows-x86_64. ...
- ASP.NET MVC4 ASP.NET Web API路由规则
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Web.Ht ...
- Android 自动编译、打包生成apk文件 3 - 使用SDK Ant方式
相关文章列表: < Android 自动编译.打包生成apk文件 1 - 命令行方式> < Android 自动编译.打包生成apk文件 2 - 使用原生Ant方式> &l ...
- inline函数和一般的函数有什么不同
1.比如: int g(int x) { return x + x; } int f() { return g(); } 这样f会调用g,然后g返回x + x给f,然后f继续把那个值返回给调用者. 如 ...
- Asp.Net Memcached安装配置使用、安全性
Memcached安装配置使用 一,准备 你需要有一下软件: VS.NET(05/08) SQLSERVER memcached服务器端以及客户端类 ...
- 微型 ORM 的第二篇 DapperLambda性能测试[Dapper比较篇]
由于这周比较忙,所以本来想做的性能测试,一直没时间,想想还是今天给补上吧 由于很多人都担心性能问题,封装之后跟Dapper的性能差距是多少,今天我给出我的测试方法,仅供参考. 创建IDbConnect ...
- mysql的触发器
删除触发器 drop TRIGGER 触发器名字; 查找库里面的所有触发器 SELECT * FROM information_schema.`TRIGGERS`;show triggers 触发器语 ...
- JQuery初识
一.什么是JQuery JQuery官方网站上是这样解释的:JQuery是一个快速简洁的JavaScript库,它可以简化HTML文档的元素遍历.事件处理.动画及Ajax交互,快速开发We ...
- Node爬虫
Node爬虫 参考 http://www.cnblogs.com/edwardstudy/p/4133421.html 所谓的爬虫就是发送请求,并将响应的数据做一些处理 只不过不用浏览器来发送请求 需 ...