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"
  1. /**
  2. * @param {string} s
  3. * @param {number} numRows
  4. * @return {string}
  5. */
  6. var convert = function (s, numRows) {
  7. if (numRows === 1) return s;
  8. let dataRow = new Array(numRows).fill('');
  9. let rowNum = 1;
  10. let moveDown = true;
  11. for (let i = 0, lens = s.length; i < lens; i++) {
  12. dataRow[rowNum - 1] += s[i];
  13. if (moveDown) {
  14. if (rowNum === numRows) {
  15. moveDown = false;
  16. }
  17. } else {
  18. if (rowNum === 1) {
  19. moveDown = true;
  20. }
  21. }
  22. if (moveDown) rowNum++;
  23. else rowNum--;
  24. }
  25. let result = '';
  26. for (row of dataRow) {
  27. //console.log(row);
  28. result += row;
  29. }
  30. return result;
  31. };

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. php速成_day4

    一.微信公众平台概述 1.微信发展史 1)2011年1月21日,腾讯推出微信应用程序.(张小龙) 2)2012年8月20日,腾讯推出微信公众平台功能,同年11月开放第三方接口 3)2013年11月注册 ...

  2. 移除手机端a标签点击自动出现的边框和背景

    手机端a标签会自动补充出现边框或者背景,使得用户知道a标签的点击状态,但样式很不好看 <!DOCTYPE html> <html> <head> <meta ...

  3. Samba 文件共享

    介绍:用于Linux系统与Windows系统之间共享文件资源,小文件可以使用lrzsz,大文件可以使用samba 测试环境: [root@localhost home]# cat /etc/redha ...

  4. RepeatSubmitInterceptor extends HandlerInterceptorAdapter

    package com.ruoyi.framework.interceptor; import java.lang.reflect.Method; import javax.servlet.http. ...

  5. PAT Advanced 1010 Radix(25) [⼆分法]

    题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...

  6. eclipse Java EE 与 Java 区别

    1. 综述 eclipse IDE 一般来说有三种可切换的模式 Java EE Java 调试 可直接下拉至底部看两者的比较. 2. Java Java 是带有用户界面的 基本IDE ,缺少数据库和w ...

  7. p2p gossip 结构化 非结构化

    p2p P2P中文名字叫对等网络,网络中节点地位一致.    QQ其实不算P2P,因为QQ利用了中央服务器.   Hbase这样的分布式系统,因为有Hmaster节点,也不算是P2P网络:   cas ...

  8. Vue专题-组件

    vue.js既然是框架,那就不能只是简单的完成数据模板引擎的任务,它还提供了页面布局的功能.本文详细介绍使用vue.js进行页面布局的强大工具,vue.js组件系统. Vue.js组件系统 每一个新技 ...

  9. bzoj2127happiness(最小割)

    一眼最小割. 一种比较好想的建图方式如下: 连源点表示学文,连汇点表示学理,然后adde(S,id(i,j),a[i][j]),adde(id(i,j),T,b[i][j]):对于相邻座位选择同一科的 ...

  10. rpm包管理工具

    介绍: RPM [1]  是Red-Hat Package Manager(RPM软件包管理器)的缩写,这一文件格式名称虽然打上了RedHat的标志,但是其原始设计理念是开放式的,现在包括OpenLi ...