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字型的形状不 ...
随机推荐
- shell脚本之 给PNG图片添加后缀@3x
1,给png图片加上后缀@3x #!/bin/sh #root_src=$(dirname $(PWD)) #echo ${root_src} image_path=${root_src}/image ...
- iOS 允许后台任务吗?
个人整理 1,用户层: 低电量模式 App后台数据刷新 的开关会影响App后台运行 2, 10分钟时间 后台任务: 在AppDelegate中加入以下代码:不受1影响 - (void)applic ...
- Requests 源码阅读笔记
models.py class Request(RequestHooksMixin): def __init__(self, method=None, url=None, headers=None, ...
- LD_RUN_PATH和LD_LIBRARY_PATH是干什么的?
1. 使用场合 LD_RUN_PATH在链接时使用 LD_LIBRARY_PATH在执行时使用 2. 如何指定环境变量 export LD_LIBRARY_PATH=/opt/jello/lib:$L ...
- win32 汇编学习(2):消息框
这一次,我们将用汇编语言写一个 Windows 程序,程序运行时将弹出一个消息框并显示"你好,我的第一个Win32汇编程序". 理论知识 Windows 为编写应用程序提供了大量的 ...
- java 之 schema解析
一,schema约束 *dtd语法:<ELEMENT 元素名 约束> *schema符合xml的语法,xml语句 **一个xml中可以有多个schema,多个schema使用名称空间区分( ...
- netty基础
1,ServerBootstrap [Bootstrap]
- ExceptionLogger
应用1:webconfig.cs中设置 public static class WebApiConfig { public static void Register(HttpConfiguration ...
- Shell脚本(三)
摘自:菜鸟教程 http://www.runoob.com/linux/linux-shell-echo.html Shell命令 1. echo命令 字符串输出 echo "OK! \c& ...
- 【译】第39节---EF6-数据库命令日志
原文:http://www.entityframeworktutorial.net/entityframework6/database-command-logging.aspx 本节,我们学习如何记录 ...