国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

LeetCode //2723. Add Two Promises (Day 30 of LC JavaScript Challenage)

這篇具有很好參考價(jià)值的文章主要介紹了LeetCode //2723. Add Two Promises (Day 30 of LC JavaScript Challenage)。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

2723. Add Two Promises

Given two promises promise1 and promise2, return a new promise. promise1 and promise2 will both resolve with a number. The returned promise should resolve with the sum of the two numbers.

?

Example 1:

Input:
promise1 = new Promise(resolve => setTimeout(() => resolve(2), 20)),
promise2 = new Promise(resolve => setTimeout(() => resolve(5), 60))
Output: 7
Explanation: The two input promises resolve with the values of 2 and 5 respectively. The returned promise should resolve with a value of 2 + 5 = 7. The time the returned promise resolves is not judged for this problem.

Example 2:

Input:
promise1 = new Promise(resolve => setTimeout(() => resolve(10), 50)),
promise2 = new Promise(resolve => setTimeout(() => resolve(-12), 30))
Output: -2
Explanation: The two input promises resolve with the values of 10 and -12 respectively. The returned promise should resolve with a value of 10 + -12 = -2.

Constraints:
  • promise1 and promise2 are promises that resolve with a number

From: LeetCode
Link: 2723. Add Two Promises
文章來源地址http://www.zghlxwxcb.cn/news/detail-502099.html


Solution:

Ideas:
This function takes two promises as input, and returns a new promise that resolves with the sum of the two numbers. The function works by first chaining the two promises together. This means that when the first promise resolves, the second promise will be executed. Once the second promise resolves, the sum of the two numbers will be calculated and the new promise will be resolved.
Code:
/**
 * @param {Promise} promise1
 * @param {Promise} promise2
 * @return {Promise}
 */
var addTwoPromises = async function(promise1, promise2) {
    return new Promise((resolve, reject) => {
        promise1.then((num1) => {
            promise2.then((num2) => {
                resolve(num1 + num2);
            });
        });
    });   
};

/**
 * addTwoPromises(Promise.resolve(2), Promise.resolve(2))
 *   .then(console.log); // 4
 */

到了這里,關(guān)于LeetCode //2723. Add Two Promises (Day 30 of LC JavaScript Challenage)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • leetcode Median of Two Sorted Arrays

    Given two sorted arrays? nums1 ?and? nums2 ?of size? m ?and? n ?respectively, return? the median ?of the two sorted arrays. The overall run time complexity should be? O(log (m+n)) . Example 1: Example 2: Constraints: nums1.length == m nums2.length == n 0 = m = 1000 0 = n = 1000 1 = m + n = 2000 -106 = nums1[i], nums2[i] = 106

    2023年04月08日
    瀏覽(25)
  • LeetCode //C - 4. Median of Two Sorted Arrays

    Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)) . ? Example 1: Input: nums1 = [1,3], nums2 = [2] Output: 2.00000 Explanation: merged array = [1,2,3] and median is 2. Example 2: Input: nums1 = [1,2], nums2 = [3,4] Output: 2.50000 Explanati

    2024年02月06日
    瀏覽(25)
  • LeetCode --- 1880. Check if Word Equals Summation of Two Words 解題報(bào)告

    The? letter value ?of a letter is its position in the alphabet? starting from 0 ?(i.e.? \\\'a\\\' - 0 ,? \\\'b\\\' - 1 ,? \\\'c\\\' - 2 , etc.). The? numerical value ?of some string of lowercase English letters? s ?is the? concatenation ?of the? letter values ?of each letter in? s , which is then? converted ?into an integer. For example, if? s = \\\"acb\\\" , we

    2024年02月13日
    瀏覽(29)
  • 【算法】Add Two Numbers 兩數(shù)相加

    給你兩個(gè) 非空 的鏈表,表示兩個(gè)非負(fù)的整數(shù)。它們每位數(shù)字都是按照 逆序 的方式存儲(chǔ)的,并且每個(gè)節(jié)點(diǎn)只能存儲(chǔ) 一位 數(shù)字。 請你將兩個(gè)數(shù)相加,并以相同形式返回一個(gè)表示和的鏈表。 你可以假設(shè)除了數(shù)字 0 之外,這兩個(gè)數(shù)都不會(huì)以 0 開頭。 每個(gè)鏈表中的節(jié)點(diǎn)數(shù)在范圍

    2024年02月11日
    瀏覽(54)
  • 算法練習(xí)Day30 (Leetcode/Python-動(dòng)態(tài)規(guī)劃)

    算法練習(xí)Day30 (Leetcode/Python-動(dòng)態(tài)規(guī)劃)

    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日
    瀏覽(26)
  • LeetCode 1. Two Sum 兩數(shù)之和

    題目描述 給定一個(gè)整數(shù)數(shù)組 nums?和一個(gè)目標(biāo)值 target,請你在該數(shù)組中找出和為目標(biāo)值的那?兩個(gè)?整數(shù),并返回他們的數(shù)組下標(biāo)。 你可以假設(shè)每種輸入只會(huì)對應(yīng)一個(gè)答案。但是,數(shù)組中同一個(gè)元素不能使用兩遍。 示例: 給定 nums = [2, 7, 11, 15], target = 9 因?yàn)?nums[0] + nums[1] = 2

    2023年04月25日
    瀏覽(24)
  • TWO DAY | WEB安全之OWASP TOP10漏洞

    TWO DAY | WEB安全之OWASP TOP10漏洞

    TWO DAY | WEB安全之OWASP TOP10漏洞 OWASP:開放式Web應(yīng)用程序安全項(xiàng)目(Open Web Application Security Project),OWASP是一家國際性組織機(jī)構(gòu),并且是一個(gè)開放的、非盈利組織,它致力于協(xié)助政府、企業(yè)開發(fā)、升級(jí)各類應(yīng)用程序以保證其可信任性。所有OWASP的工具、文檔、研討以及所有分會(huì)都對

    2024年02月12日
    瀏覽(30)
  • leetcode 2446. Determine if Two Events Have Conflict

    You are given two arrays of strings that represent two inclusive events that happened on the same day, event1 and event2, where: event1 = [startTime1, endTime1] and event2 = [startTime2, endTime2]. Event times are valid 24 hours format in the form of HH:MM. A conflict happens when two events have some non-empty intersection (i.e., some moment is common to bo

    2024年02月22日
    瀏覽(23)
  • 【每日一題Day282】LC2681英雄力量 | 排序+數(shù)學(xué)

    給你一個(gè)下標(biāo)從 0 開始的整數(shù)數(shù)組 nums ,它表示英雄的能力值。如果我們選出一部分英雄,這組英雄的 力量 定義為: i0 , i1 ,… ik 表示這組英雄在數(shù)組中的下標(biāo)。那么這組英雄的力量為 max(nums[i0],nums[i1] ... nums[ik])2 * min(nums[i0],nums[i1] ... nums[ik]) 。 請你返回所有可能的 非空

    2024年02月14日
    瀏覽(22)
  • 【算法】Maximum Sum of Two Non-Overlapping Subarrays 兩個(gè)非重疊子數(shù)組的最大和

    問題 有一個(gè)整數(shù)數(shù)組nums,和2個(gè)整數(shù)firstlen,secondlen,要求找出2個(gè)非重疊子數(shù)組中的元素的最大和,長度分別是firstlen,secondlen。 不限制2個(gè)子數(shù)組的先后順序。 firstlen,secondlen的范圍 [ 1 , 1000 ] [1,1000] [ 1 , 1000 ] firstlen+secondlen的范圍 [ 2 , 1000 ] [2,1000] [ 2 , 1000 ] f i r s t l e n ,

    2023年04月27日
    瀏覽(354)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包