当前位置: 首页 > news >正文

LeetCode 6: ZigZag Conversion

The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

P   A   H   N
A P L S I I G
Y   I   R

And then read line by line: “PAHNAPLSIIGYIR”

Write the code that will take a string and make this conversion given a number of rows:

string convert(string s, int numRows);
Example 1:

Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"

Example 2:

Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"

Explanation:

P     I    N
A   L S  I G
Y A   H R
P     I

基本想法就是先填充一个二维矩阵,再遍历矩阵拼接字符。

class Solution {
    public String convert(String s, int numRows) {
        int len = s.length();
        if (len <= 1 || numRows == 1){
            return s;
        }
        int period = 2*numRows-2;
        char[][] mat = new char[numRows][(len/period+1)*(numRows-1)];

        for(int i = 0; i < len; ++i){
            if(i%period < numRows){
                mat[i%period][(i/period)*(numRows-1)] = s.charAt(i);
            }else{
                mat[period-i%period][(i/period)*(numRows-1)+i%period-numRows+1] = s.charAt(i);
            }
        }
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < numRows; ++i){
            for (int j = 0; j < (len/period+1)*(numRows-1); ++j){
                if(mat[i][j] != 0){
                    sb.append(mat[i][j]);
                }
            }
        }
        return sb.toString();
    }
}

相关文章:

  • 一段时间没来,blog升级了?
  • Google 的怪异域名大全
  • 机器学习中的方差偏差分析(Bias-variance analysis)
  • 新爹手记-分娩篇-生男生女
  • Web环境下SVG地图浏览器的设计与实现
  • 基于SVG的WebGIS地图搜索研究与实现
  • 程序员教你不背单词学英语!流利英语一周成!!!
  • 工业标准的矢量图像格式----SVG
  • 一个关于遗传算法的java小实验(吃豆人)
  • Nvidia副总裁称“CPU已死,GPU当立”
  • 宽度学习(Broad Learning System)
  • Mock in JUnit
  • 用概率告诉你:集齐 “五福” 要多久
  • 矩阵测度(L2诱导范数对应下的矩阵测度)
  • 垃圾邮件30年
  • extract-text-webpack-plugin用法
  • httpie使用详解
  • JavaScript函数式编程(一)
  • Mocha测试初探
  • Mysql数据库的条件查询语句
  • Python学习笔记 字符串拼接
  • React+TypeScript入门
  • React16时代,该用什么姿势写 React ?
  • Vue源码解析(二)Vue的双向绑定讲解及实现
  • Xmanager 远程桌面 CentOS 7
  • 翻译--Thinking in React
  • 浮现式设计
  • 后端_ThinkPHP5
  • 解决iview多表头动态更改列元素发生的错误
  • 开源中国专访:Chameleon原理首发,其它跨多端统一框架都是假的?
  • 前端学习笔记之观察者模式
  • 深度学习中的信息论知识详解
  • 想写好前端,先练好内功
  • 【运维趟坑回忆录 开篇】初入初创, 一脸懵
  • PostgreSQL之连接数修改
  • RDS-Mysql 物理备份恢复到本地数据库上
  • ​软考-高级-系统架构设计师教程(清华第2版)【第20章 系统架构设计师论文写作要点(P717~728)-思维导图】​
  • # Apache SeaTunnel 究竟是什么?
  • #162 (Div. 2)
  • $(selector).each()和$.each()的区别
  • (C++)八皇后问题
  • (Matalb分类预测)GA-BP遗传算法优化BP神经网络的多维分类预测
  • (附源码)计算机毕业设计ssm-Java网名推荐系统
  • (六)什么是Vite——热更新时vite、webpack做了什么
  • (论文阅读26/100)Weakly-supervised learning with convolutional neural networks
  • (三)终结任务
  • (十六)串口UART
  • (转)全文检索技术学习(三)——Lucene支持中文分词
  • (转贴)用VML开发工作流设计器 UCML.NET工作流管理系统
  • .360、.halo勒索病毒的最新威胁:如何恢复您的数据?
  • .bat批处理(八):各种形式的变量%0、%i、%%i、var、%var%、!var!的含义和区别
  • .NET/C# 反射的的性能数据,以及高性能开发建议(反射获取 Attribute 和反射调用方法)
  • .Net转Java自学之路—基础巩固篇十三(集合)
  • @transaction 提交事务_【读源码】剖析TCCTransaction事务提交实现细节
  • []FET-430SIM508 研究日志 11.3.31