[leetcode]ZigZag Conversion @ Python
原题地址:https://oj.leetcode.com/problems/zigzag-conversion/
题意:
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"
.
解题思路:字符串处理,没什么知识点,需要一点编程技巧。
代码:
class Solution:
# @return a string
def convert(self, s, nRows):
if nRows==1: return s
tmp=['' for i in range(nRows)]
index=-1; step=1
for i in range(len(s)):
index+=step
if index==nRows:
index-=2; step=-1
elif index==-1:
index=1; step=1
tmp[index]+=s[i]
return ''.join(tmp)
[leetcode]ZigZag Conversion @ Python的更多相关文章
- 6.[leetcode] ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- LeetCode ZigZag Conversion(将字符串排成z字型)
class Solution { public: string convert(string s, int nRows) { string a=""; int len=s.leng ...
- LeetCode: ZigZag Conversion 解题报告
ZigZag ConversionThe string "PAYPALISHIRING" is written in a zigzag pattern on a given num ...
- leetcode6:Zigzag Conversion@Python
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 like ...
- LeetCode——ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- [LeetCode] ZigZag Conversion [9]
称号 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- C++ leetcode::ZigZag Conversion
mmp,写完没保存,又得重新写.晚上写了简历,感觉身体被掏空,大学两年半所经历的事,一张A4纸都写不满,真是一事无成呢.这操蛋的生活到底想对我这个小猫咪做什么. 今后要做一个早起的好宝宝~晚起就诅咒自 ...
- Leetcode:ZigZag Conversion分析和实现
问题的大意就是将字符串中的字符按锯齿状(倒N形)垂直由上向下放置,最后水平从左向右读取.比如 ABCDEFGHIJKLMN,4表示 A G M B F H ...
随机推荐
- Python 项目实践三(Web应用程序) 第三篇
接着上节的继续学习,现在要显示所有主题的页面 有了高效的网页创建方法,就能专注于另外两个网页了:显示全部主题的网页以及显示特定主题中条目的网页.所有主题页面显示用户创建的所有主题,它是第一个需要使用数 ...
- MOXA的Nport5600初始密码
今天第一次弄Nport,看了半天手册没找到初始密码,网上也搜不到,按照说明书上想打电话问问,发现根本是空号... 后来灵感一来试了一下,居然是:moxa
- BugkuCTF ---游戏过关 writeup
下载程序后发现是输入1-8数字中的一位 至于怎么玩,我到现在都没弄懂 不多说,直接拖入Ollydbg 搜索 分析了一下字符串,发现有一个显示flag的字符串 双击过去看看,发现类似于一个函数. 接下来 ...
- 关于dubbo服务的xml配置文件报错的问题
在配置dubbo服务的过程中,经常会遇到虽然程序能够跑起来,但是配置文件一堆红叉,虽然不影响功能,但是确实很让人恶心. 报错信息如下: Multiple annotations found at th ...
- 用css解决table文字溢出控制td显示字数(转)
场景: 最左边这栏我不行让他换行,怎么办呢? 下面是解决办法: table{ width:100px; table-layout:fixed;/* 只有定义了表格的布局算法为fixed,下面td的定义 ...
- SGU 200. Cracking RSA (高斯消元求自由变元个数)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=200 200. Cracking RSA time limit per test: ...
- 【Java】须要配置的三个Java环境变量
我的电脑→属性→高级系统设置→高级→环境变量 1.JAVA_HOME : JDK的安装路径 2.PATH : %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin; 3.CLASSP ...
- layer.confirm 询问框 的层遮盖
function admin_del(obj) { layer.confirm('确认要重启吗?', { btn : [ '确定', '取消' ]//按钮 }, function(index) { l ...
- [Deepin 15] sudo source /etc/profile 提示找不到 source 命令(切换到 root 用户:sudo su)
在 Deepin/Ubuntu 系统 中,因为修改了下 配置文件,然后执行 source 命令重新加载配置文件,结果: sudo source /etc/profile 提示找不到 source 命令 ...
- python dtrace 安装与应用
https://ipfans.github.io/2016/09/tracing-python-program-with-dtrace/?utm_source=tuicool&utm_medi ...