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 ...
随机推荐
- 一线大厂的分布式唯一ID生成方案是什么样的?
本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...
- 中小规模集群----Centos6部署wordpress及java程序
1 概述 1.1 业务需求 公司共有两个业务,网上图书馆和一个电商网站.现要求运维设计一个安全架构,本着高可用.廉价的原则. 具体情况如下: 网上图书馆是基于jsp开发: 电商系统是基于 ...
- 离群点检测(Novelty Detection, Outlier Detenction)
适合问题: 对于无标签的数据, 又想找出坏用户,完成业务目标. 参考: https://scikit-learn.org/stable/modules/outlier_detection.html 算 ...
- Centos8无法安装screen的解决方法:使用epel安装screen
选择了一个基础款的vps安装的镜像选了熟悉的centos的最新版centos8,但是在安装screen的时候,却安装不了,提示: No match for argument: screen 本来以为是 ...
- error: snap "electronic-wechat" has "install-snap" change in progress
今天因为要使用 wechat ,但是因为 wechat 并没有官方的 Ubuntu 版本,幸好有大神出了 electronic-wechat ,可以直接在应用商店中搜到,然后直接安装,也可以命令行安装 ...
- hasura graphql-engine v1.2.0 beta 版本
hasura graphql-engine v1.2.0 提供了一个很不错的功能action,这个也是目前其他graphql 没有hasura 强大的 地方,使用action 我们可以更好的扩展has ...
- py02_03:py的数据类型
数据类型初识 1. 整数(int) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483647 在64位系统上,整数的位数为64位,取值 ...
- 洛谷 P2278 [HNOI2003]操作系统
题目传送门 解题思路: 一道没啥思维含量的模拟题,但是个人感觉代码实现不简单,可能是我太弱了,花了我6个小时,3次重写. AC代码: #include<iostream> #include ...
- Linux中的各种文件类型
Linux中有一句话:一切皆是文件 1.普通文件( - regular file ) (1).文本文件 文件中的内容是由文本构成的,文本指的是ASCII码字符.文件里的内容本质上都是数字( ...
- PANIC: ANDROID_SDK_HOME is defined but could not find Nexus_5_API_23.ini file in $ANDROID_SDK_HOME\
运行模拟器总是出现这个错误 后来把系统环境变量中的ANDROID_SDK_HOME 删掉就好了 我去,好神奇的操作