Algorithm problem: sum of two numbers -- JavaScript and Java implementation

A cup of green tea 2021-02-23 02:34:08
algorithm problem sum numbers javascript


Given an array of integers nums And a target value target , Find two integers in the array whose target value is sum , And return their array subscripts .

JavaScript Realization

Used ES6 Medium Map object , The code is as follows :

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 Realization

Use HashMap Realization , The code is as follows :

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]
}
}
版权声明
本文为[A cup of green tea]所创,转载请带上原文链接,感谢
https://qdmana.com/2021/02/20210222230331055Q.html

  1. An inexperienced front-end engineer, what are the common problems when writing CSS?
  2. HttpServletRequest get URL (parameter, path, port number, protocol, etc.) details
  3. Springboot starts http2
  4. Enabling http2.0 in spring boot
  5. JQuery:JQuery基本语法,JQuery选择器,JQuery DOM,综合案例 复选框,综合案例 随机图片
  6. Using JavaScript in Safari browser history.back () the page will not refresh after returning to the previous page
  7. vue.js Error in win10 NPM install
  8. In less than two months, musk made more than $1 billion, more than Tesla's annual profit
  9. Springboot starts http2
  10. Vue event bus
  11. JQuery easy UI tutorial: custom data grid Pagination
  12. Using okhttp and okhttpgo to obtain onenet cloud platform data
  13. Vue3 component (IX) Vue + element plus + JSON = dynamic rendering form control
  14. HTTP 1. X learning notes: an authoritative guide to Web Performance
  15. Vue3 component (IX) Vue + element plus + JSON = dynamic rendering form control
  16. HTTP 1. X learning notes: an authoritative guide to Web Performance
  17. JQuery:JQuery基本语法,JQuery选择器,JQuery DOM,综合案例 复选框,综合案例 随机图片
  18. Event bubble and capture in JavaScript
  19. The root element is missing solution
  20. Event bubble and capture in JavaScript
  21. Configure the certificate to enable ngnix to publish the trusted website of HTTPS
  22. Javascript数据类型
  23. HTTP interface debugging tool! 48000 star HTTP command line client!
  24. Parameter encryption of front end URL link band
  25. HTTP interface debugging tool! 48000 star HTTP command line client!
  26. Three front end frameworks: data binding and data flow
  27. Reading Axios source code (1) -- exploring the realization of basic ability
  28. Event bubble and capture in JavaScript
  29. 【微前端】微前端最終章-qiankun指南以及微前端整體探索
  30. R & D solution e-Car front end monitoring system
  31. [JS] 877 - 35 wonderful knowledge of JavaScript, long experience!
  32. R & D solution e-Car front end monitoring system
  33. High performance nginx HTTPS tuning - how to speed up HTTPS by 30%
  34. 解决ajax跨域问题【5种解决方案】
  35. Top ten classic sorting of JavaScript
  36. HTTP 1. X learning notes: an authoritative guide to Web Performance
  37. Vue3 component (IX) Vue + element plus + JSON = dynamic rendering form control component
  38. My http / 1.1 is so slow!
  39. Why Vue uses asynchronous rendering
  40. The response status was 0. Check out the W3C XMLHttpRequest Level 2 spec for
  41. The tapable instance object hook of webpack4. X core tool library
  42. The tapable instance object hook of webpack4. X core tool library
  43. Using libcurl for HTTP communication in C + +
  44. Using libcurl for HTTP communication in C + +
  45. Using CSS variable in Vue
  46. Deeply understand the update of state and props in react
  47. No matter how fast the Internet is, it's useless! In addition to Baidu disk, there is this website slow to let you eat shriveled
  48. Baidu share does not support the solution of HTTPS
  49. [micro front end] the final chapter of micro front end - Qiankun guide and overall exploration of micro front end
  50. [micro front end] the final chapter of micro front end - Qiankun guide and overall exploration of micro front end
  51. Vue cli creates vue3 project
  52. Nginx reverse proxy for windows authentication using NTLM
  53. Rust tutorial: introduction to rust for JavaScript developers
  54. Deploying personal blog to Tencent cloud with serverless framework
  55. R & D solution e-Car front end monitoring system
  56. JavaScript advanced learning
  57. Spend 1 minute to master these 5 ppt tips, courseware making less detours
  58. Vue: vuex persistent state
  59. React native gets the current network state of the device Netinfo
  60. High performance nginx HTTPS tuning - how to speed up HTTPS by 30%