[勇者闯LeetCode] 6. ZigZag Conversion

Description

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".

Information

  • Tags: String
  • Difficulty: Medium

Solution

找规律
1. 所有行的重复周期都是 2 * (nRows - 1)
2. 对于首行和末行之间的行,还会额外重复一次,重复周期是 2 * (nRows - i - 1),其中i表示第i行。

class Solution(object):def convert(self, s, numRows):""":type s: str:type numRows: int:rtype: str"""if numRows < 2: return sans = ""for i in range(0, numRows):for j in range(i, len(s), 2*(numRows-1)):ans += s[j]if i > 0 and i < numRows-1 and (j + 2 * (numRows-i-1)) < len(s):ans += s[j + 2 * (numRows-i-1)]return ans

[勇者闯LeetCode] 6. ZigZag Conversion相关推荐

  1. [勇者闯LeetCode] 1. Two Sum

    [勇者闯LeetCode] 1. Two Sum Description Given an array of integers, return indices of the two numbers s ...

  2. [勇者闯LeetCode] 70. Climbing Stairs

    [勇者闯LeetCode] 70. Climbing Stairs Description You are climbing a stair case. It takes n steps to rea ...

  3. [勇者闯LeetCode] 112. Path Sum

    [勇者闯LeetCode] 112. Path Sum Description Given a binary tree and a sum, determine if the tree has a r ...

  4. [勇者闯LeetCode] 191. Number of 1 Bits

    [勇者闯LeetCode] 191. Number of 1 Bits Description Write a function that takes an unsigned integer and ...

  5. [勇者闯LeetCode] 83. Remove Duplicates from Sorted List

    [勇者闯LeetCode] 83. Remove Duplicates from Sorted List Description Given a sorted linked list, delete ...

  6. [勇者闯LeetCode] 38. Count and Say

    [勇者闯LeetCode] 38. Count and Say Description The count-and-say sequence is the sequence of integers b ...

  7. [勇者闯LeetCode] 189. Rotate Array

    [勇者闯LeetCode] 189. Rotate Array Description Rotate an array of n elements to the right by k steps. F ...

  8. [勇者闯LeetCode] 190. Reverse Bits

    [勇者闯LeetCode] 190. Reverse Bits Description Reverse bits of a given 32 bits unsigned integer. For ex ...

  9. [勇者闯LeetCode] 169. Majority Element

    [勇者闯LeetCode] 169. Majority Element Description Given an array of size n, find the majority element. ...

最新文章

  1. 每日一题 -- 11-1
  2. BZOJ 2137 submultiple(约数,拉格朗日插值求自然数k次幂和)【BZOJ 修复工程】
  3. JVM调优常用参数配置
  4. 神经网络总结(初稿)
  5. echart高级使用_Echart使用总结
  6. 【51单片机快速入门指南】4.5:I2C 与 TCA6416实现双向 IO 扩展
  7. 战队服务器人员位置,和平精英:职业战队如何分配成员位置?5大定位划出5种职位...
  8. dj电商-需求分析-商品模块
  9. Javascript 操作元素Class属性的问题
  10. Matplotlib Line2D设置
  11. Hibernate中的一对一映射
  12. python计时器timeit返回秒数_一日一技:Python中的timeit()方法
  13. 基于DEAP库的python进化算法-5.遗传算法求解TSP问题的改进
  14. Vue 系列二 之 猛龙行动之绝密代码
  15. Chainmaker 查询当前区块高度
  16. python lxml_python – lxml使用命名空间而不是ns0,ns1,
  17. 最新delphi7序列号和破解文件
  18. FPGA零基础学习:图像显示系统设计
  19. 长假之后,Scrum团队应该修改Sprint的结束时间吗?
  20. Android 学习博客

热门文章

  1. JAVA 读写锁中锁降级的必要性
  2. 3d打印的方向及成本问题
  3. 新手入门:快速搭建3D可视化场景(下)
  4. An enum switch case label must be the unqualified name of an enumeration cons
  5. manjaro安装python_manjaro安装以及配置
  6. VS 2019 + Win10 + VMware 双机调试
  7. 计算机语言dim,字面量
  8. SketchUp模型组件【iMod · 精选243 —— 现代轻奢卧室SU模型】
  9. Apache Shiro漏洞检测(CVE-2016-4437)
  10. Low-Code,一定“low”吗?