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的更多相关文章

  1. [LeetCode] Zigzag Iterator 之字形迭代器

    Given two 1d vectors, implement an iterator to return their elements alternately. For example, given ...

  2. [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 ...

  3. [LeetCode] ZigZag Converesion 之字型转换字符串

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  4. 【leetcode】ZigZag Conversion

    题目简述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...

  5. 整数压缩编码 ZigZag

    在分析Avro源码时,发现Avro为了对int.long类型数据压缩,采用Protocol Buffers的ZigZag编码(Thrift也采用了ZigZag来压缩整数). 1. 补码编码 为了便于后 ...

  6. No.006:ZigZag Conversion

    问题: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  7. ZigZag Conversion leetcode java

    题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  8. 【leetcode❤python】 6. ZigZag Conversion

    #-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object):    def convert(self, s, numRow ...

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

随机推荐

  1. Jenkins + GitLab 通过 Webhook 自动触发构建爬坑记录

    前言   在局域网搭建了一个Jenkins服务,用于自动构建和发布,在调通了构建程序之后,想使用内网的GitLab的Webhook功能触发代码推送事件,然后进行自动构建.后来发现在GitLab上做测试 ...

  2. mongodb的学习-3-在Mac上的安装配置

    1.使用homebrew安装: brew install mongodb 查看安装好的版本: mongo --version MongoDB shell version v3.6.4 git vers ...

  3. Docker删除/停止容器

    应用场景:某个相关的业务需要重启,容器太多了,一个一个通过命令行来关闭太麻烦了,直接一条命令直接搞定. 命令如下: $ docker ps // 查看所有正在运行容器 $ docker stop co ...

  4. C# 实现动态加载DLL插件 及HRESULT:0x80131047处理

    本代码实现DLL的动态加载, 类似PS里的滤镜插件! 1. 建立一个接口项目类库,此处名称为:Test.IPlugin using System; namespace Test.IPlugin { p ...

  5. Threadpool python3

    from concurrent.futures import ThreadPoolExecutor,ALL_COMPLETED,wait,as_completedimport time def add ...

  6. 如何解析json字符串及返回json数据到前端

    前言:最近需要实现的任务是:写若干个接口,并且接口中的请求数据是json格式,然后按照请求参数读取前端提前整理好的json数据,并且将json数据返回到服务器端. 主要的工具:Gson  2.8.2 ...

  7. 解决yum安装 openssl-devel时产生的Multilib version problems found错误(转)

    Error: Multilib version problems found. This often means that the root cause is something else and m ...

  8. .Net实现Word文档及导出

    参考网址: http://www.jb51.net/article/25062.htm(实用性) http://wenku.baidu.com/link?url=44O7Dua49DrZ-PF2QU7 ...

  9. C++ Primer 学习笔记_45_STL实践与分析(19)--泛型算法的结构

    STL实践与分析 --泛型算法的结构 引言: 正如全部的容器都建立在一致的设计模式上一样,算法也具有共同的设计基础. 算法最主要的性质是须要使用的迭代器种类.全部算法都指定了它的每一个迭代器形參可使用 ...

  10. 【css】css实现点击Toggle功能/icon切换

    ①实现Toggle功能 html结构: <div class="cssTog"><label> <p> 专业:B020309现代企业管理(独立本 ...