Leetcode_6. Zigzag convertion
6. Zigzag convertion
对输入的字符串做锯齿形变换,并输出新的字符串,所谓zigzag变化如下图所示。

将"ABCDEFGHIJKL"做4行的锯齿变换,新的字符串为:AGBFHLCEIKDJ"
实现一个根据输入字符串s与行数numRows的函数。
思路:
根据输入的行数创建一个字符串slice, slice一个元素表示该行的所有字符,最后将所有slice元素相加,即为新的字符串。
func zigzag_convert(s string, numRows int) string {
if numRows == 1 {
return s
}
var str string
tmp := make([]string, numRows)
for i, c := range s {
j := i % (numRows*2 - 2)
if j < numRows {
tmp[j] += string(c)
} else {
tmp[numRows*2-2-j] += string(c)
}
}
for _, str1 := range tmp {
str += str1
}
return str
}
Leetcode_6. Zigzag convertion的更多相关文章
- [LeetCode] Zigzag Iterator 之字形迭代器
Given two 1d vectors, implement an iterator to return their elements alternately. For example, given ...
- [LeetCode] Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- [LeetCode] ZigZag Converesion 之字型转换字符串
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 ...
- 整数压缩编码 ZigZag
在分析Avro源码时,发现Avro为了对int.long类型数据压缩,采用Protocol Buffers的ZigZag编码(Thrift也采用了ZigZag来压缩整数). 1. 补码编码 为了便于后 ...
- No.006:ZigZag Conversion
问题: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- ZigZag Conversion leetcode java
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- 【leetcode❤python】 6. ZigZag Conversion
#-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object): def convert(self, s, numRow ...
- Binary Tree Zigzag Level Order Traversal [LeetCode]
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
随机推荐
- 私有仓库harbor安装包括https
1. 下载离线的 harbor gz包 wget https://github.com/vmware/harbor/releases/download/v1.2.0/harbor-offline-in ...
- [luogu2668] 斗地主
题面 这好像就是道**暴搜题, 由于可以回溯, 所以顺序其实没有多大的关系, 见代码吧... 具体代码 #include <iostream> #include <cstring ...
- 利用Python实现12306爬虫--查票
在上一篇文章(http://www.cnblogs.com/fangtaoa/p/8321449.html)中,我们实现了12306爬虫的登录功能,接下来,我们就来实现查票的功能. 其实实现查票的功能 ...
- mongodb启动与运用
在操作前需要启动mongodb数据库服务 1.首先打开dos窗口,然后选择路径到你的安装路径下的bin目录(我的路径是的D:mongo\mongodb\bin) 2.然后输入启动命令(D:mongo\ ...
- 定义抽象类Shape,抽象方法为showArea(),求出面积并显示,定义矩形类Rectangle,正方形类Square,圆类 Circle,根据各自的属性,用showArea方法求出各自的面积,在main方法中构造3个对象,调用showArea方法。(体现多态)
实现多态的三个条件:1.要有继承2.要有抽象方法重写3.用父类指针(引用)指向子类对象 重载重写重定义的区别: 1.重载:在同一个类中进行; 编译时根据参数类型和个数决定方法调用; 子类无法重载父类; ...
- Pat乙级1011题:A+B和C
题目:给定区间[-2的31次方, 2的31次方]内的3个整数A.B和C,请判断A+B是否大于C. 我写的代码: del abc(self,a,b,c,i): if a+b>c: print(&q ...
- SQL AND & OR 运算符
AND 和 OR 运算符用于基于一个以上的条件对记录进行过滤. AND 和 OR 运算符 AND 和 OR 可在 WHERE 子语句中把两个或多个条件结合起来. 假设第一个条件和第二个条件都成立,则 ...
- TFTP服务的搭建
TFTP服务的作用:提供网络下载服务 tftp服务器的安装与配置: tftp主要用于嵌入式交叉开发环境的搭建,传输文件. 0.创建tftp的工作目录,并修改权限(注意:请在主目录下创建此工作目录!) ...
- 【转】CSDN离线网页html文件自动跳转
问题: 最近使用OneNote2016剪辑csdn的文章时,发现一些公式/文本框不能被正确识别,所以离线保存网页的html文件. 但是每次打开html文件,都会自动跳转的CSDN主页,即使断网,也会自 ...
- 兼容性/pollyfill/shim/渐进增强/优雅降级
http://ued.ctrip.com/blog/browser-compatibility-testing-tools-in-firefox-compatibility-detector.html ...