将一个给定字符串根据给定的行数,以从上往下、从左到右进行 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)的更多相关文章

  1. leetcode 6 z字型变换

    执行用时 :64 ms, 在所有 Python3 提交中击败了99.74%的用户由题目可知 我们的最终字符串会被摆成 numRows 行,那我们理解为 最终结果是numRows个字符串相加 先建立等于 ...

  2. [LeetCode] Z字型变换

    题目内容: 将字符串 "PAYPALISHIRING" 以Z字形排列成给定的行数: P A H N A P L S I I G Y I R 之后从左往右,逐行读取字符:" ...

  3. leetcode 6/300 Z字型变换 py

    目录 题目说明 方法一:利用flag 题目说明 方法一:利用flag 简单来说就是利用flag来表示方向,真的神来之笔. class Solution: def convert(self, s: st ...

  4. 06. Z字型变换

    题目: 提交01: class Solution { public String convert(String s, int numRows) { int length = 2*numRows-2; ...

  5. LeetCode OJ:ZigZag Conversion(字符串的Z字型转换)

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

  6. 算法:Z字型(Zigzag)编排

    问题:给定 n 行和 m 列的二维数组矩阵.如图所示,以 ZIG-ZAG 方式打印此矩阵. 从对称的角度来看,通过反复施加滑行反射可以从简单的图案如线段产生规则的之字形. 主要思想:算法从(0, 0) ...

  7. LeetCode ZigZag Conversion(将字符串排成z字型)

    class Solution { public: string convert(string s, int nRows) { string a=""; int len=s.leng ...

  8. 281. Zigzag Iterator z字型遍历

    [抄题]: Given two 1d vectors, implement an iterator to return their elements alternately. Example: Inp ...

  9. YTU 2898: C-Z型变换

    2898: C-Z型变换 时间限制: 1 Sec  内存限制: 128 MB 提交: 53  解决: 15 题目描述 让我们来玩个Z型变换的游戏,游戏的规则如下: 给你一个字符串,将它以Z字型的形状不 ...

随机推荐

  1. shell脚本之 给PNG图片添加后缀@3x

    1,给png图片加上后缀@3x #!/bin/sh #root_src=$(dirname $(PWD)) #echo ${root_src} image_path=${root_src}/image ...

  2. iOS 允许后台任务吗?

    个人整理 1,用户层: 低电量模式 App后台数据刷新 的开关会影响App后台运行 2,   10分钟时间 后台任务: 在AppDelegate中加入以下代码:不受1影响 - (void)applic ...

  3. Requests 源码阅读笔记

    models.py class Request(RequestHooksMixin): def __init__(self, method=None, url=None, headers=None, ...

  4. LD_RUN_PATH和LD_LIBRARY_PATH是干什么的?

    1. 使用场合 LD_RUN_PATH在链接时使用 LD_LIBRARY_PATH在执行时使用 2. 如何指定环境变量 export LD_LIBRARY_PATH=/opt/jello/lib:$L ...

  5. win32 汇编学习(2):消息框

    这一次,我们将用汇编语言写一个 Windows 程序,程序运行时将弹出一个消息框并显示"你好,我的第一个Win32汇编程序". 理论知识 Windows 为编写应用程序提供了大量的 ...

  6. java 之 schema解析

    一,schema约束 *dtd语法:<ELEMENT 元素名 约束> *schema符合xml的语法,xml语句 **一个xml中可以有多个schema,多个schema使用名称空间区分( ...

  7. netty基础

    1,ServerBootstrap  [Bootstrap] 

  8. ExceptionLogger

    应用1:webconfig.cs中设置 public static class WebApiConfig { public static void Register(HttpConfiguration ...

  9. Shell脚本(三)

    摘自:菜鸟教程 http://www.runoob.com/linux/linux-shell-echo.html Shell命令 1. echo命令 字符串输出 echo "OK! \c& ...

  10. 【译】第39节---EF6-数据库命令日志

    原文:http://www.entityframeworktutorial.net/entityframework6/database-command-logging.aspx 本节,我们学习如何记录 ...