Code-2-js

Question:

Given an array of integers, returnindicesof 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 thesameelement twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9,return [0,1].


Answer:

- method 1

note: this is more obvious, but less efficient;

- method 2

note: using a new object makes the code more efficient with only one loop~~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,112評(píng)論 0 23
  • 這該死的雨竟然下了整整一天 不過(guò)這火鍋倒是最應(yīng)景兒的
    AI大叔閱讀 264評(píng)論 0 0
  • 正如那花 你期待著用心灌溉 總不見(jiàn)花開(kāi) 偶爾路過(guò)的風(fēng)景 卻讓自己心生感慨 生活就是這樣 越追求越離初心漸道漸遠(yuǎn) 缺...
    不惑而歌閱讀 164評(píng)論 0 2
  • 十月是蒼白的寒意, 是孤獨(dú) 是北方 我喜歡四月,卻活在十月 絢麗的南方 櫻花和驕陽(yáng) 但櫻花是陌生的東西,驕陽(yáng)也從未...
    丿肆悅閱讀 172評(píng)論 0 1

友情鏈接更多精彩內(nèi)容