LeetCode--006--Z字型变换(java)
将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。
比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下:
L C I R
E T O E S I I G
E D H N
之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"LCIRETOESIIGEDHN"。
请你实现这个将字符串进行指定行数变换的函数:
string convert(string s, int numRows);
示例 1:
输入: s = "LEETCODEISHIRING", numRows = 3
输出: "LCIRETOESIIGEDHN"
示例 2:输入: s = "LEETCODEISHIRING", numRows = 4 输出: "LDREOEIIECIHNTSG" 解释:
L D R
E O E I I
E C I H N
T S G 分析:
竖着看,看每个字符属于第几行,每一行为一个string,最后把每一行都连接起来。行索引index和总行数的关系为index = i %(2 * rowNums - 2),
当index小于总行数,index不变,否则index = 2 * rowNums - 2 - index,比如第二列的c,index = 2 * 4 - 2 - 4 = 2;
class Solution {
public String convert(String s, int numRows) {
if(numRows<=1)return s;
StringBuilder[] sb = new StringBuilder[numRows];
for(int i = 0;i < sb.length;i++){
sb[i] = new StringBuilder("");
}
for(int i = 0;i <s.length();i++){
int index = i %(2 * numRows - 2);
index = index < numRows ? index : 2*numRows - 2 - index;
sb[index].append(s.charAt(i));
}
for(int i = 1;i < sb.length;i++){
sb[0].append(sb[i]);
}
return sb[0].toString();
}
}
2019-03-13 22:52:42
LeetCode--006--Z字型变换(java)的更多相关文章
- leetcode 6 z字型变换
执行用时 :64 ms, 在所有 Python3 提交中击败了99.74%的用户由题目可知 我们的最终字符串会被摆成 numRows 行,那我们理解为 最终结果是numRows个字符串相加 先建立等于 ...
- [LeetCode] Z字型变换
题目内容: 将字符串 "PAYPALISHIRING" 以Z字形排列成给定的行数: P A H N A P L S I I G Y I R 之后从左往右,逐行读取字符:" ...
- leetcode 6/300 Z字型变换 py
目录 题目说明 方法一:利用flag 题目说明 方法一:利用flag 简单来说就是利用flag来表示方向,真的神来之笔. class Solution: def convert(self, s: st ...
- 06. Z字型变换
题目: 提交01: class Solution { public String convert(String s, int numRows) { int length = 2*numRows-2; ...
- LeetCode OJ:ZigZag Conversion(字符串的Z字型转换)
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 算法:Z字型(Zigzag)编排
问题:给定 n 行和 m 列的二维数组矩阵.如图所示,以 ZIG-ZAG 方式打印此矩阵. 从对称的角度来看,通过反复施加滑行反射可以从简单的图案如线段产生规则的之字形. 主要思想:算法从(0, 0) ...
- LeetCode ZigZag Conversion(将字符串排成z字型)
class Solution { public: string convert(string s, int nRows) { string a=""; int len=s.leng ...
- 281. Zigzag Iterator z字型遍历
[抄题]: Given two 1d vectors, implement an iterator to return their elements alternately. Example: Inp ...
- YTU 2898: C-Z型变换
2898: C-Z型变换 时间限制: 1 Sec 内存限制: 128 MB 提交: 53 解决: 15 题目描述 让我们来玩个Z型变换的游戏,游戏的规则如下: 给你一个字符串,将它以Z字型的形状不 ...
随机推荐
- vsCode设置中文
1.安装软件之后,关闭欢迎界面,Ctrl+shift+p打开命令窗口,输入lang,选择configuration display language,改为 "locale":&qu ...
- ODAC(V9.5.15) 学习笔记(八)TOraScript
名称 类型 说明 DataSet 如果脚本中返回了数据结果,则通过该数据集进行获取 Delimiter string 脚本语句之间的分隔符 EndLine Integer 脚本中最后一行的行号 End ...
- C# Math类简介运用
总结了一下几个常用的Math类 /* ######### ############ ############# ## ########### ### ###### ##### ### ####### ...
- 我的互联网30年。永远的8U8 永远的Y365
我的互联网30年.永远的8U8 永远的Y365
- Matplotlib 知识点整理
本文作为学习过程中对matplotlib一些常用知识点的整理,方便查找. 强烈推荐ipython 无论你工作在什么项目上,IPython都是值得推荐的.利用ipython --pylab,可以进入Py ...
- centos6.8下搭建编译openwrt的环境
1. 安装必要软件 su root yum install zlib-devel zlib-static -y 2. 编译openwrt 请参考这里
- mysql的数据类型- 特别是表示日期/时间的数据类型: 参考: http://www.cnblogs.com/bukudekong/archive/2011/06/27/2091590.html
通常认为: 日期 就是 年-月-日: 时间就是: 小时:分钟:秒 要严格区分"日期"和 "时间"的 说法. 日期就是日期, 时间就是时间, 两者是不同的!! 日 ...
- Concepts-->Migrations
https://flywaydb.org/documentation/migrations Overview With Flyway all changes to the database are c ...
- 【教程】Git在Eclipse中的安装和基本使用
一.安装 点击 Help->Install New Software->add 安装地址为:http://download.eclipse.org/egit/updates/ 选择插件 ...
- Centos 安装R
1 下载R源代码 原码下载地址https://cloud.r-project.org/https://cloud.r-project.org/src/base/R-3/R-3.4.2.tar.gz 2 ...