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 ...
随机推荐
- ElasticSearch 获取es信息以及索引操作
检查集群的健康情况 GET /_cat/health?v green:每个索引的primary shard和replica shard都是active状态的yellow:每个索引的primary sh ...
- 译:ORCFILE IN HDP 2:更好的压缩,更高的性能
原文地址: https://hortonworks.com/blog/orcfile-in-hdp-2-better-compression-better-performance/ ORCFILE I ...
- .Net文档下载
public static void DownLoadFile(string fileName, HttpResponse Response, HttpServerUtility Server) { ...
- java框架复习 简单介绍 (转载)
一.SpringMVC http://blog.csdn.net/evankaka/article/details/45501811 Spring Web MVC是一种基于Java的实现了Web MV ...
- warning:ISO C90 forbids mixed declarations and code
warning:ISO C90 forbids mixed declarations and code 变量定义之前不论什么一条非变量定义的语句(重视:语句是会带分号的)都会引起这个警告! 将非变量的 ...
- SQL进阶语法的多表操作
AS别名 多张表联合操作,如果表多,字段名长,不方便阅读.这里我们可以使用 as 关键字来对字段名设置别名. as也可以省略,看个人喜好,在这里我还是支持把 as 写上,这样我们在面对复杂的SQL ...
- PHP操作xml学习笔记之增删改查(1)—增加
xml文件 <?xml version="1.0" encoding="utf-8"?><班级> <学生> ...
- hadoop3.1.0 HDFS快速搭建伪分布式环境
1.环境准备 CenntOS7环境 JDK1.8-并配置好环境变量 下载Hadoop3.1.0二进制包到用户目录下 2.安装Hadoop 1.解压移动 #1.解压tar.gz tar -zxvf ha ...
- 20155211 2016-2017-2 《Java程序设计》第九周学习总结
20155211 2016-2017-2 <Java程序设计>第九周学习总结 教材学习内容总结 第十六章 整合数据库 一.JDBC入门 (一)JDBC简介 厂商在操作JDBC驱动程序时,依 ...
- 2017-2018-2 《网络对抗技术》 20155322 Exp 5 MSF基础应用
[-= 博客目录 =-] 1-实践目标 1.1-实践介绍 1.2-实践内容 1.3-实践要求 2-实践过程 2.1-情报收集 2.2-主动攻击实践-ms08_067 2.3-浏览器攻击实践-many* ...