算法题:两数之和——JavaScript及Java实现

一杯绿茶 2021-02-22 23:03:35
java 算法 javascript 实现 之和


给定一个整数数组 nums 和一个目标值 target ,在该数组中找出和为目标值的两个整数,并返回他们的数组下标。

JavaScript 实现

使用了 ES6 中的 Map 对象,代码如下:

const nums = [2, 7, 11, 15],
target = 9;
function sumofTwoNumber(nums, target) {
let map = new Map();
for(let i=0; i<nums.length; i++) {
let k = target - nums[i];
if(map.has(k)) {
return [map.get(k), i]
}
map.set(nums[i], i)
}
return []
}
const res = sumofTwoNumber(nums, target);
console.log(res); // [0, 1]

Java 实现

使用 HashMap 实现,代码如下:

package com.company;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
public class SumOfTwoNumberUsingHashMap {
public static List<Integer> sumOfTwoNumber(List<Integer> nums, Integer target) {
HashMap<Integer, Integer> map = new HashMap<>();
for(int i=0; i<nums.size(); i++) {
int k = target - nums.get(i);
if(map.containsKey(k)) {
List<Integer> list = new ArrayList<>();
list.add(map.get(k));
list.add(i);
return list;
}
map.put(nums.get(i), i);
}
List<Integer> list = new ArrayList<>();
return list;
}
public static void main(String[] args) {
List<Integer> nums = new ArrayList<>();
nums.add(2);
nums.add(7);
nums.add(11);
nums.add(15);
List<Integer> res = sumOfTwoNumber(nums, 9);
System.out.println(res); // [0, 1]
}
}
版权声明
本文为[一杯绿茶]所创,转载请带上原文链接,感谢
https://segmentfault.com/a/1190000039256480

  1. How to insert an element into the specified index of an array?
  2. 配置证书使得ngnix能够发布https的可信网站
  3. HTTP接口调试利器!4.8万Star的HTTP命令行客户端!
  4. 前端url链接带的参数加密
  5. HTTP接口调试利器!4.8万Star的HTTP命令行客户端!
  6. Is react server components OK?
  7. Summary of front end basic knowledge (4) - webpack
  8. Sass nesting rule
  9. 前端三大框架:数据绑定与数据流
  10. axios 源码阅读(一)--探究基础能力的实现
  11. Javascript中的事件冒泡与捕获
  12. #研发解决方案#易车前端监控系统
  13. 【JS】877- 35 个 JavaScript 的奇葩知识,长见识了!
  14. #研发解决方案#易车前端监控系统
  15. 高性能 Nginx HTTPS 调优 - 如何为 HTTPS 提速 30%
  16. Front end, school recruitment, Taobao, guide
  17. Front end, social recruitment, Taobao, guide
  18. javascript 十大经典排序
  19. Draw a mellow cactus with the boneless technique of Meticulous Brushwork
  20. HTTP 1.x 學習筆記 —— Web 效能權威指南
  21. Vue3元件(九)Vue + element-Plus + json = 動態渲染的表單控制元件
  22. 我的 HTTP/1.1 好慢啊!
  23. Vue為何採用非同步渲染
  24. The response status was 0. Check out the W3C XMLHttpRequest Level 2 spec for
  25. Front end monastery
  26. How to quickly understand a new front end project?
  27. webpack4.X核心工具库之tapable实例对象Hook
  28. webpack4.X核心工具库之tapable实例对象Hook
  29. C++使用libcurl进行http通讯
  30. Can be directly used in HTML special character table Unicode character set
  31. C++使用libcurl进行http通讯
  32. Java traverses list < string > and takes out the string elements in it, and splices them with ","
  33. Self taught web front end more than two months, talk about my harvest and progress, learning summary
  34. vue 中使用 css 变量
  35. 深入了解React中state和props的更新
  36. 百度分享不支持https的解决方案
  37. [practical] ABAP mail sending (HTML + attachment)
  38. [practical] ABAP mail sending (HTML + attachment)
  39. Teach you how to select products with less profit and competition in Amazon Product Development (1)
  40. 【微前端】微前端最终章-qiankun指南以及微前端整体探索
  41. 【微前端】微前端最终章-qiankun指南以及微前端整体探索
  42. Vue-Cli 创建vue3项目
  43. Go in the front of the progress of u boot v7.0 U disk boot disk production tools
  44. 使用NTLM的windows身份验证的nginx反向代理
  45. Rust教程:针对JavaScript开发人员的Rust简介
  46. 使用 Serverless Framework 部署个人博客到腾讯云
  47. #研發解決方案#易車前端監控系統
  48. Vue changes localhost to IP address and cannot access
  49. JavaScript进阶学习
  50. HTML5 from entry to proficient, realize annual salary 10W +, zero basic students must see
  51. Vue:vuex状态数据持久化插件vuex-persistedstate
  52. Vue source code analysis - start
  53. Vue -- the child component calls the method of the parent component and passes parameters --- props
  54. React-Native 获取设备当前网络状态 NetInfo
  55. 高性能 Nginx HTTPS 调优 - 如何为 HTTPS 提速 30%
  56. How to learn HTML5? How can Xiaobai start HTML5 quickly?
  57. HTML + CSS detailed tutorial, this article is enough, but also quickly save
  58. JavaScript高级:JavaScript面向对象,JavaScript内置对象,JavaScript BOM,JavaScript封装
  59. Why Vue uses asynchronous rendering
  60. JavaScript高级:JavaScript面向对象,JavaScript内置对象,JavaScript BOM,JavaScript封装