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 ...
随机推荐
- 【node.js】函数、路由
Node.js中函数的使用与Javascript类似,一个函数可以作为另一个函数的参数.我们可以先定义一个函数,然后传递,也可以在传递参数的地方直接定义函数. function say(word) { ...
- Kafka设计解析(八)Exactly Once语义与事务机制原理
转载自 技术世界,原文链接 Kafka设计解析(八)- Exactly Once语义与事务机制原理 本文介绍了Kafka实现事务性的几个阶段——正好一次语义与原子操作.之后详细分析了Kafka事务机制 ...
- 解决JS在url中传递参数时参数包含中文乱码的问题
1.传参页面JavaScript代码: function go_mark(id,jobname,headimgurl,nickname){ window.location.href = "m ...
- 总结js基础方法
//判断对象上是否有个这个属性 hasProreturn obj != null && hasOwnProperty.call(obj, key); //判断是不是布尔值 isBool ...
- 使用Navicat for Oracle工具连接oracle
使用Navicat for Oracle工具连接oracle的 这是一款oracle的客户端的图形化管理和开发工具,对于许多的数据库都有支持.之前用过 Navicat for sqlserver,感觉 ...
- Jquery中的height(),innerHeight(),outerHeight()的区别
前言 最近练习做弹窗,遇到height(),innerHeight(),outerHeight()的区别. 根据下面的盒模型来了解三者的区别. height():element的height; inn ...
- SQL 练习一 字符型函数
处理字符串时,利用字符型函数的嵌套组合是非常有效的,试分析一道考题: create table customers(cust_name varchar2(20)); insert into custo ...
- iOS开发过程中易犯的小错误
addGestureRecognizer(_:) 一个手势对象只绑定一个view // 只有最后一个imgv有点击事件 let tap = UITapGestureRecognizer(target: ...
- mysql索引和外键
innodb外键: 1.CASCADE:从父表删除或更新会自动删除或更新子表中匹配的行 2.SET NULL:从父表删除或更新行,会设置子表中的外键列为NULL,但必须保证子表列没有指定NOT NUL ...
- rails使用Kindeditor网页编辑器
在gemfile中加入(后面版本别丢) gem 'rails_kindeditor', '~> 0.5.0' $ bundle 创建配置文件,并且引入js rails g rails_kinde ...