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~~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,496评论 0 23
  • 这该死的雨竟然下了整整一天 不过这火锅倒是最应景儿的
    AI大叔阅读 1,558评论 0 0
  • 正如那花 你期待着用心灌溉 总不见花开 偶尔路过的风景 却让自己心生感慨 生活就是这样 越追求越离初心渐道渐远 缺...
    不惑而歌阅读 1,229评论 0 2
  • 十月是苍白的寒意, 是孤独 是北方 我喜欢四月,却活在十月 绚丽的南方 樱花和骄阳 但樱花是陌生的东西,骄阳也从未...
    丿肆悦阅读 1,265评论 0 1

友情链接更多精彩内容