LeetCode 6.Z 字形变换
2019-03-29 22:06
162 查看
题目描述:将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。之后,你的输出需要从左往右逐行读取,产生出一个新的字符串。(请对照原题)
解:题解里两个都是按行的思路,没学过java就没仔细看,我就直接按列把原字符串变成Z字形了,找到周期T然后写条件判断是拐角,最后读出来就行了,好像还挺快的;需要注意的是由于输出的是新的字符串,不是这个Z字形,所以Z字形里的空格都不需要考虑,我一开始没有意识到这一点,还想了老半天Python怎么在列表里添加空元素。。
class Solution: def convert(self, s: str, numRows: int) -> str: if numRows == 1: return s else: a = ['' for j in range(numRows)] T = 2 * numRows - 2 for i in range(len(s)): h = i % T if h < numRows: a[h] = a[h] + s[i] else: a[T - h] = a[T - h] + s[i] return ''.join(a)
(80ms, 13.4MB) ~ (100%, 8.23%),Python3,引起舒适
相关文章推荐
- 【Leetcode】6.Z字形变换
- @LeetCodeZ字形变换--ZigZag Conversion[C++]
- Leetcode(C++)——6. Z 字形变换
- leetCode第6题,Z字形变换
- leetcode-6倒转Z 字形变换
- LeetCode 6 Z字形变换(字符串)
- LeetCode-6 : Z 字形变换 ★★★
- LeetCode题解(python)-6. Z 字形变换
- Z 字形变换(LeetCode)
- 基础算法之Z字形变换(Leetcode-6)
- LeetCode6. Z 字形变换
- 【LeetCode】之字形变换
- leetcode Z 字形变换
- Z字形变换 - LeetCode
- LeetCode第6题--Z 字形变换
- 【leetcode】Zigzag Z 字形变换
- leetcode 6. Z 字形变换(Java版)
- LeetCode | 6. ZigZag Conversion(之字形变换)
- LeetCode6.python实现:Z 字形变换问题☆☆
- leetcode解题之103. Binary Tree Zigzag Level Order Traversal java版(之字形打印二叉树)