LeetCode Day 6
- 将一个给定字符串根据给定的行数,以从上往下、从左到右进行 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的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [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 ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- 萤火虫系统(firefly) RK3399 python3 安装 spicy
最近一直在研究深度学习,公司里正好也有个项目是需要在RK3399的板子上跑人脸识别. 原本在linux_x86系统上轻轻松松装的环境到了RK3399板子上(firefly系统)安装起来真的非常麻烦,需 ...
- [C/C++]C/C++计算代码的运行时间
有很多时候,实现一个功能后可能不仅仅要效果,还要效率,如果可以在极短的时间内完成一个功能那当然是最好不过的啦,但是可能经常会事与愿违. 这里就写一下,都可以怎样用C/C++或者Qt的方法来测试代码的运 ...
- IE浏览器F12调试模式不能使用或报错以及安装程序遇到错误0x80240037的解决办法
记录一下,方便以后查找 IE浏览器F12调试模式不能使用: 需要下载补丁: 64位系统 然后下载安装,就能解决问题. 要是在安装时遇到出现: 安装程序遇到错误 0x80240037 解决方式 最后 ...
- git配置报错fatal: Authentication failed for ''问题解决
如果在git配置中报错fatal: Authentication failed for '',其实就是凭证失败的意思 接着输入一下命令行没有出现要求输入用户名或密码,并报错 $ git config ...
- POJ-2561 Network Saboteur(DFS)
题目: A university network is composed of N computers. System administrators gathered information on t ...
- UML-领域模型-如何找到概念类
有3个方法 方法1:对已有的重用和修改(这是最好的方法) 重用和修改现有模型.这些模型来源于之前的项目.网上的 方法2:使用分类列表 从网上搜索该领域的常见分类,或参考书籍Martin Fowler的 ...
- java使用io流实现图片复制
import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException; public cl ...
- c#学习笔记03——委托和事件
委托:一种引用类型,这种类型可以用来定义方法签名,从而使用委托实现将方法作为参数传递给其他方法.类似于C++中的函数之争,使用委托使程序员可以将方法引用封装在委托对象内. 定义和声明委托: deleg ...
- mysql数据库5.6.45安装后的配置(离线安装包版)
二.windows10下的配置 (1) 环境变量配置 打开控制面板=>系统和安全=>系统=>高级系统设置,选择环境变量,在系统变量中找到path,编辑该选项. 第一行是oracle数 ...
- Servlet&JSP复习笔记 01
1. Servlet 含义:服务器端的小程序,它只是服务器中的一部分. Servlet Little 标准:Sun公司制定的一种用来扩展Web服务器功能的组件规范. a. 扩展web服务器功能:扩展w ...