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 ...
随机推荐
- linux下ffmpeg环境搭建记录
1.Linux下安装yasm 官网下载:http://yasm.tortall.net/Download.html tar -zvxf yasm-1.3.0.tar.gz cd yasm-1.3.0/ ...
- sqlserver2008的sql语句支持的最大长度
想写一个sql语句,很长,主要是in后跟着无数个用户ID,(虽然实现方式很低级,但是还是凑合着用吧) 不知道sql最大长度是多少,看了 SQL Server 的最大容量规范,写的是 包含 SQL 语句 ...
- Log4Net 使用及组合公共类
好记性不如烂笔头,这次是由衷的感受到了! log4net 是一个很好用的日志记录工具,引用入项目中,如何查看项目内部运行情况,如何快速定位异常信息,好的日志记录能帮很大的忙: log4net 很好用, ...
- 基于libcurl的GET与POST(HTTP1.1)
#include <stdio.h> #include <curl/curl.h> bool getUrl(char *filename) { CURL *curl; CURL ...
- 201512-1 数位之和 Java
思路: mod 10获取最低位,除以10去掉最低位 import java.util.Scanner; public class Main { public static void main(Stri ...
- UML-如何画操作契约?
1.在编写契约过程中,发现之前的领域模型不对,此时是否需要修改? 需要修改.包括:概念类.属性.关联.这就是不断迭代和进化 2.用例中复杂场景里的状态变化细节,描述过多导致用例臃肿,让人看不下去,因此 ...
- nginx 报错Malformed HTTP request line, git 报错fatal: git-write-tree: error building trees
nginx 报错由于url里有空格,包括url本身或者参数有空格 git 报错是因为解决冲突的时候没有add,即没有merge
- pandas 学习笔记【持续更新】
import numpy as np import pandas as pd import matplotlib.pyplot as plt df1 = pd.DataFrame(np.arange( ...
- Microsoft .NET Framework 4.5.2 (Offline Installer)
Microsoft .NET Framework 4.5.2 (Offline Installer) for Windows Vista SP2, Windows 7 SP1, Windows 8, ...
- 7.windows-oracle实战第七课 --约束、索引
数据的完整性 数据的完整性用于确保数据库数据遵从一定的商业和逻辑规则.数据的完整性使用约束.触发器.函数的方法来实现.在这三个方法中,约束易于维护,具备最好的性能,所以作为首选. 约束:not nu ...