LeetCode0006

  • 将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。
  • 比如输入字符串为 "LEETCODEISHIRING" ,指定行数为 3 时,排列如下:
L C I R
E T O E S I I G
E D H N
  • 之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"LCIRETOESIIGEDHN"。
  • 示例 1:
  • 输入: s = "LEETCODEISHIRING", numRows = 3
  • 输出: "LCIRETOESIIGEDHN"
/**
* @param {string} s
* @param {number} numRows
* @return {string}
*/
var convert = function (s, numRows) {
if (numRows === 1) return s;
let dataRow = new Array(numRows).fill('');
let rowNum = 1;
let moveDown = true;
for (let i = 0, lens = s.length; i < lens; i++) {
dataRow[rowNum - 1] += s[i];
if (moveDown) {
if (rowNum === numRows) {
moveDown = false;
}
} else {
if (rowNum === 1) {
moveDown = true;
}
}
if (moveDown) rowNum++;
else rowNum--;
}
let result = '';
for (row of dataRow) {
//console.log(row);
result += row;
}
return result;
};

LeetCode Day 6的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  10. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. MySQL--索引和外键

    来自:http://www.jb51.net/article/32149.htm 1.添加PRIMARY KEY(主键索引)  ALTER TABLE `table_name` ADD PRIMARY ...

  2. vue中报错Props with type Object/Array must use a factory function to return the default value

    Invalid default value for prop "value": Props with type Object/Array must use a factory fu ...

  3. windows服务器搭建SVN[多项目设置方法]

    https://tortoisesvn.net/downloads.html 根据系统版本进行下载,下载后正常一路正常安装. 第一.设置版本号仓库目录,比如:cdengine 第二.在cdengine ...

  4. 垃圾windows10更新遇到的问题

    缘由 1.win10现在必须更新,不更新不给你用,关闭自动更新的方法都失效了,如果有人知道有效的方法还请私信指教一下.. 一个延迟几天的笨方法:当出现更新并关机或更新并重启时,把电源键设置成关机. 就 ...

  5. java通过HSSFWorkbook导出xls文件

    使用swgger2.Restlet等接口工具有bug导致导出失败,测试直接使用浏览器 //导出列表-新 @UserRoleJudgment(authpos = SystemControllerLog. ...

  6. git 推送到github最常用命令

    初始化仓库,上传到github中. git init git add README.md git commit -m "first commit" git remote add o ...

  7. 痢疾杆菌|SARS

    病原微生物分析: Eg:痢疾杆菌 测序---比对(K12vs治病菌1vs治病菌2),发现: “毒力岛”:K12没有,两个治病菌有,缩小搜寻范围. “黑洞”:K12有,但两个治病菌没有. SARS: 构 ...

  8. 面向对象 part5

    构造函数模式与原型模式结合 function Person(name) = { this.name = name this.friends = ["a", "b" ...

  9. 洛谷 P5017 摆渡车

    题目传送门 解题思路: 个人感觉DP这东西,只可意会,不可言传 AC代码: #include<iostream> #include<cstdio> #include<cs ...

  10. 嵌入式Linux环境变量如何参与程序运行

    1.环境变量一共有两份,一份在Flash中,另一份在DDR中.uboot开机时一次性从Flash中读取全部环境变量到DDR中作为环境变量的初始化值,然后使用过程中都是用DDR这一份,用户可以用save ...