成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

LeetCode 01 || twoSum

sutaking / 2298人閱讀

摘要:有一個整數(shù)數(shù)組,返回其中兩個值之和為指定值的索引。遍歷數(shù)組每個元素,獲與每個元素的差值,然后利用數(shù)組的方法在剩余的數(shù)組值中查找差值,如果有,則將當前索引與方法查找的索引保存在數(shù)組中,返回。初版,總算完成了。

two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the same element twice.

有一個整數(shù)數(shù)組,返回其中兩個值之和為指定值的索引。假設(shè)每個輸入指定值只有一個解,然后不能使用同一個元素兩次

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
第一版

思路:先想到的肯定是兩次循環(huán)。遍歷數(shù)組每個元素,獲target與每個元素的差值,然后利用數(shù)組的indexOf()方法在剩余的數(shù)組值中查找差值,如果有,則將當前索引與indexOf()方法查找的索引保存在數(shù)組中,返回。

    /**
     * @param {number[]} nums
     * @param {number} target
     * @return {number[]}
     */
    var twoSum = function(nums, target) {
        var result = [];
        for(var i=0; i

結(jié)論:耗時335ms,只有17%的beats。。。初版,總算完成了。

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/81923.html

相關(guān)文章

  • [Leetcode] 3Sum 4Sum 3Sum Closet 多數(shù)和

    摘要:為了避免得到重復(fù)結(jié)果,我們不僅要跳過重復(fù)元素,而且要保證找的范圍要是在我們最先選定的那個數(shù)之后的。而計算則同樣是先選一個數(shù),然后再剩下的數(shù)中計算。 2Sum 在分析多數(shù)和之前,請先看Two Sum的詳解 3Sum 請參閱:https://yanjia.me/zh/2019/01/... 雙指針法 復(fù)雜度 時間 O(N^2) 空間 O(1) 思路 3Sum其實可以轉(zhuǎn)化成一個2Sum的題,...

    trigkit4 評論0 收藏0
  • [LeetCode] 170. Two Sum III - Data structure desig

    Problem Design and implement a TwoSum class. It should support the following operations: add and find. add - Add the number to an internal data structure.find - Find if there exists any pair of number...

    dack 評論0 收藏0
  • LeetCode 之 JavaScript 解答第一題 —— 兩數(shù)之和(Two Sum)

    摘要:步驟遍歷數(shù)組數(shù)據(jù),將根據(jù)下標和元素值存放到散列表中。目標值減去數(shù)組元素差值并在散列表中查找。測試法三一遍哈希表算法思路遍歷目標值減去數(shù)組元素的差值同時判斷該值在散列表中是否存在差值,如果存在,則返回否則將數(shù)據(jù)加入到散列表中。 Time:2019/4/1Title:Two SumDifficulty: simpleAuthor:小鹿 題目一:Two Sum Given an array ...

    k00baa 評論0 收藏0
  • [Leetcode] Two Sum 兩數(shù)和

    摘要:如果存在該差值,說明存在兩個數(shù)之和是目標和。而哈希表方法中的則可以換成。如果要求的不是兩個數(shù)和和,而是找兩個數(shù)之差為特定值的配對呢同樣用哈希表可以解決。 Two Sum Given an array of integers, find two numbers such that they add up to a specific target number.The function t...

    pkhope 評論0 收藏0
  • LeetCode 167:兩數(shù)之和 II - 輸入有序數(shù)組 Two Sum II - Input a

    摘要:公眾號愛寫給定一個已按照升序排列的有序數(shù)組,找到兩個數(shù)使得它們相加之和等于目標數(shù)。函數(shù)應(yīng)該返回這兩個下標值和,其中必須小于。示例輸入輸出解釋與之和等于目標數(shù)。 公眾號: 愛寫bug(ID:icodebugs) 給定一個已按照升序排列 的有序數(shù)組,找到兩個數(shù)使得它們相加之和等于目標數(shù)。 函數(shù)應(yīng)該返回這兩個下標值 index1 和 index2,其中 index1 必須小于 index2。...

    張春雷 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<