https://leetcode.com/problems/zigzag-conversion/
来源:互联网 发布:java web中启动线程池 编辑:程序博客网 时间:2024/06/03 19:13
https://leetcode.com/problems/zigzag-conversion/
要锯齿状的输出
首先分组
如果nRows为m 那么纵列 m 个斜 m-2个 因此 每2m-2个字符是一组 每组占据m-1列
并根据这样算出要初始化的数组list的大小
1 A 2 8B 3 7 C G46 DF 5 E 1-8算一组 A-G算不完整的最后一组接着按照要求 输出字符即可 用while len>2m-2 之前整组输出 最后一组单独输出 因为长度没办法输出完整租
最后将list种的字符按行输出即可
class Solution: # @param {string} s # @param {integer} numRows # @return {string} def convert(self, s, numRows): #初始化 m=numRows if m==1: return s elif m==2: string='' for i in range(len(s)): if i%2==0: string=string+s[i] for i in range(len(s)): if i%2==1: string=string+s[i] return string else: #2m-2 多少个数分一组 a=len(s)/(2*m-2)#每组占多少列 b=len(s)%(2*m-2)#最后不足一组的长度 if b<=m: n=a*(m-1)+1 else: n=a*(m-1)+b-m+1 #m行n列 list=[([0] * n) for i in range(m)] index=0 while len(s)>=2*m-2: #输出列 m个 for i in range(m): list[i][index*(m-1)]=s[i] #输出斜 for i in range(m-2): list[m-i-2][index*(m-1)+i+1]=s[i+m] index=index+1 s=s[2*m-2:] else:#剩下的不足以输出整个组 判断个数能输出列否 斜否 if len(s)<=m:#只列 for i in range(len(s)): list[i][index*(m-1)]=s[i] else: extra=len(s)-m for i in range(m): list[i][index*(m-1)]=s[i] for i in range(extra): list[m-i-2][index*(m-1)+i+1]=s[i+m] sol='' for i in range(m): for j in range(n): if list[i][j]!=0: sol=sol+list[i][j] return sol
0 0
- https://leetcode.com/problems/zigzag-conversion/
- https://leetcode.com/problems/candy/
- https://leetcode.com/problems/sqrtx/
- https://oj.leetcode.com/problems/majority-element/
- https://leetcode.com/problems/longest-common-prefix/
- https://leetcode.com/problems/pascals-triangle/
- https://leetcode.com/problems/powx-n/
- https://leetcode.com/problems/reverse-integer/
- https://leetcode.com/problems/plus-one/
- https://leetcode.com/problems/unique-paths/
- https://leetcode.com/problems/palindrome-number/
- https://leetcode.com/problems/permutation-sequence/
- https://leetcode.com/problems/climbing-stairs/
- https://leetcode.com/problems/divide-two-integers/
- https://leetcode.com/problems/add-binary/
- https://leetcode.com/problems/add-two-numbers/
- https://leetcode.com/problems/merge-sorted-array/
- https://leetcode.com/problems/first-missing-positive/
- 检测机器的CPU的字节序(Big Endian OR Little Endian)
- 第五届在线编程大赛月赛第一题:完全平方数的个数
- webService 路径
- 【连载】Java学习系列(008)——深入面向对象-2
- Win7 AERO主题变成灰色解决方法
- https://leetcode.com/problems/zigzag-conversion/
- Java基本概念与问题(三)
- java开发环境搭建
- [刷题]Minimum Path Sum
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
- Intent的详细介绍
- 编程之美---求数组的子数组之和的最大值
- HOOK钩子技术4 SSDT HOOK
- 322A - Ciel and Dancing 322B - Ciel and Flowers