LeetCode //2649. Nested Array Generator (Day 30 of LC JavaScript Challenage)

这篇具有很好参考价值的文章主要介绍了LeetCode //2649. Nested Array Generator (Day 30 of LC JavaScript Challenage)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

2649. Nested Array Generator

Given a multi-dimensional array of integers, return a generator object which yields integers in the same order as inorder traversal.

A multi-dimensional array is a recursive data structure that contains both integers and other multi-dimensional arrays.

inorder traversal iterates over each array from left to right, yielding any integers it encounters or applying inorder traversal to any arrays it encounters.

 

Example 1:

Input: arr = [[[6]],[1,3],[]]
Output: [6,1,3]
Explanation:
const generator = inorderTraversal(arr);
generator.next().value; // 6
generator.next().value; // 1
generator.next().value; // 3
generator.next().done; // true

Example 2:

Input: arr = []
Output: []
Explanation: There are no integers so the generator doesn’t yield anything.

Constraints:
  • 0 < = a r r . f l a t ( ) . l e n g t h < = 1 0 5 0 <= arr.flat().length <= 10^5 0<=arr.flat().length<=105
  • 0 < = a r r . f l a t ( ) [ i ] < = 1 0 5 0 <= arr.flat()[i] <= 10^5 0<=arr.flat()[i]<=105
  • m a x N e s t i n g D e p t h < = 1 0 5 maxNestingDepth <= 10^5 maxNestingDepth<=105

From: LeetCode
Link: 2649. Nested Array Generator文章来源地址https://www.toymoban.com/news/detail-474351.html


Solution:

Ideas:
The idea behind this code is to use a generator function to traverse a nested array. A generator function is a function that can be used to produce a sequence of values without actually storing all of the values in memory. This can be useful for large data sets, as it prevents the entire data set from being loaded into memory at once.
The inorderTraversal function works by recursively traversing the array. The recursive function checks if the current element is an array. If it is, it calls itself recursively to traverse the array. If it is not, it yields the element. This process continues until the entire array has been traversed.
Code:
/**
 * @param {Array} arr
 * @return {Generator}
 */
var inorderTraversal = function*(arr) {
    while (arr.length) {
    const current = arr.shift();

    if (Array.isArray(current)) {
      yield* inorderTraversal(current);
    } else {
      yield current;
    }
  }
};

/**
 * const gen = inorderTraversal([1, [2, 3]]);
 * gen.next().value; // 1
 * gen.next().value; // 2
 * gen.next().value; // 3
 */

到了这里,关于LeetCode //2649. Nested Array Generator (Day 30 of LC JavaScript Challenage)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode 2475. Number of Unequal Triplets in Array【数组,排序,哈希表】简单

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月09日
    浏览(36)
  • Leetcode 2897. Apply Operations on Array to Maximize Sum of Squares

    Leetcode 2897. Apply Operations on Array to Maximize Sum of Squares 1. 解题思路 2. 代码实现 题目链接:2897. Apply Operations on Array to Maximize Sum of Squares 这一题事实上非常的简单,我们只需要想明白一些关键点就行了。 题中最终的目标是获得 k k k 个数,使得其平方和最大。因此,我们就只需要

    2024年02月07日
    浏览(37)
  • LeetCode 2496. Maximum Value of a String in an Array【字符串,数组】简单

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月11日
    浏览(40)
  • 算法练习Day30 (Leetcode/Python-动态规划)

    62. Unique Paths There is a robot on an  m x n  grid. The robot is initially located at the  top-left corner  (i.e.,  grid[0][0] ). The robot tries to move to the  bottom-right corner  (i.e.,  grid[m - 1][n - 1] ). The robot can only move either down or right at any point in time. Given the two integers  m  and  n , return  the number of possible

    2024年01月20日
    浏览(34)
  • Elasticsearch 基于 array 结构 的nested类型的索引的聚合查询

    这几天在做es的聚合查询,对那种一对多的产品数据查询的时候遇到了一些问题,做一下记录 针对每个产品名称[ product_name ]进行分组,并对预算[ budget ]求和 产品名称 预算 电视机 4000 手机 851 相机 5000 扑克牌 2 es查询语句==简化版 es查询的结果==简化版 注意电视机的doc_count为

    2024年02月16日
    浏览(46)
  • clickhouse的嵌套数据结构Tuple、Array与Nested类型介绍和使用示例

    Tuple 是 ClickHouse 数据库中的一种数据类型,它允许在一个字段中存储由不同数据类型组成的元组(tuple)。 元组可以包含任意数量的值,并且每个值可以是不同的数据类型,如 int 、 float 、 string 、 date 等。 例如,以下是一个 clickhouse Tuple 类型的例子: (1, \\\'John\\\', 12.5, Date(\\\'2021-0

    2024年02月14日
    浏览(37)
  • Javascript——生成器(Generator)自动执行

    Generator自动化是通过Thunk函数进行实现,写这篇文章的目的是为了理解通过Thunk实现Generator函数的自动执行。 我们可以带入一个业务场景来帮助我们理解Thunk实现Generator自动执行的好处,业务场景如下: 假设小明今天干了一件事情是:         1、买菜         2、买完菜回家

    2024年03月25日
    浏览(91)
  • 深入学习JavaScript系列(七)——Promise async/await generator

    本篇属于本系列第七篇 第一篇:#深入学习JavaScript系列(一)—— ES6中的JS执行上下文 第二篇:# 深入学习JavaScript系列(二)——作用域和作用域链 第三篇:# 深入学习JavaScript系列(三)——this 第四篇:# 深入学习JavaScript系列(四)——JS闭包 第五篇:# 深入学习JavaScrip

    2023年04月08日
    浏览(34)
  • 【每日一题Day282】LC2681英雄力量 | 排序+数学

    给你一个下标从 0 开始的整数数组 nums ,它表示英雄的能力值。如果我们选出一部分英雄,这组英雄的 力量 定义为: i0 , i1 ,… ik 表示这组英雄在数组中的下标。那么这组英雄的力量为 max(nums[i0],nums[i1] ... nums[ik])2 * min(nums[i0],nums[i1] ... nums[ik]) 。 请你返回所有可能的 非空

    2024年02月14日
    浏览(33)
  • 【每日一题Day168】LC2427公因子的数目 | 模拟

    给你两个正整数 a 和 b ,返回 a 和 b 的 公 因子的数目。 如果 x 可以同时整除 a 和 b ,则认为 x 是 a 和 b 的一个 公因子 。 简单模拟 感谢力扣 今天还要开会 我恨 感觉习惯真的很容易突然改变 前段时间还是看英文题目的 突然每一天就没有看英文题了 然后这个习惯就没有了

    2023年04月08日
    浏览(27)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包