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 ...
随机推荐
- 27. docker compose 单机 均衡负载
1.编写Dockerfile #Dockerfile FROM python:2.7 LABEL maintaner="eaon eaon123@docker.com" COPY ...
- 88.QuerySet API使用详解:get_or_create和bulk_create方法
get_or_create 根据某个条件进行查找,如果找到了匹配的数据就会返回这条数据,如果没有找到匹配到的数据,就会创建一个.示例代码如下: from django.http import Http ...
- 深入浅出 java.String
深入浅出 java.String Java 处理字符串常用的一些方法 Java定义一个字符串 直接定字符串 直接定义字符串表示直接使用""来表示字符串中的内容 String str ...
- 自定义的listbox支持拖放
unit unit2; interface uses Classes, Controls, StdCtrls; type TListBox2 = class(TCustomListBox) prote ...
- Linux不进入网卡配置文件更改静态ip
1.找到网卡配置文件名ls /etc/sysconfig/network-scripts/ 2.备份并查看原始配置文件(若原先有配置IP的,则按照第五点方式修改) 3.修改随机自启和IP地址echo ...
- Java 中的接口有什么作用?以及接口和其实现类的关系?
Java 中的接口有什么作用? - Ivony的回答 - 知乎 https://www.zhihu.com/question/20111251/answer/16585393 这是一个初学者非常常见的 ...
- POJ-2561 Network Saboteur(DFS)
题目: A university network is composed of N computers. System administrators gathered information on t ...
- python函数中的参数(关键字参数,默认参数,位置参数,不定长参数)
默认参数:定义函数的时候给定变量一个默认值. def num(age=1): 位置参数:调用函数的时候根据定义函数时的形参位置和实参位置进行引用. 关键字参数:如果定义的函数中含有关键字参数,调用函数 ...
- UINavigationbar跳转黑色
bug效果:导航栏过渡出现黑色
- ansible puppet saltstack三款自动化运维工具的对比
一.基础介绍 ansible基础介绍可参考:http://www.linuxidc.com/Linux/2017-12/149671.htm puppet基础介绍可参考:http://www.linu ...