Today I Learned
  • Introduction
  • Problem Solving
    • leetcode
      • 1. Two Sum
        • CPP
        • Java
        • JavaScript
        • Kotlin
        • Python
    • contest
      • google code jam
  • developer
    • ansible
    • python
      • line_by_line
      • config
        • yaml
        • configparser
  • linux
    • cut
  • Programming Languages
    • javascript
      • array
      • base64
      • canvas
      • date
        • moment.js
      • jquery
      • json
      • jsplumb
      • konva
      • nodejs
      • react
      • string
    • kotlin
      • 2 코틀린 기초.md
  • spring framework
    • spring boot
  • game
    • ingress
    • Rockman
      • Rockman X 1
      • Rockman X 2
      • Rockman X 3
  • database
    • mysql
  • me
    • resume
      • experience
      • extracurricular_activities
Powered by GitBook
On this page

Was this helpful?

  1. Problem Solving
  2. leetcode
  3. 1. Two Sum

JavaScript

array 에 넣고 검색

  • 780 ms, faster than 3.76%

  • 기존에 해 왔던 방식이랑 거의 비슷한데 등수가 썩 좋지는 않음.

var twoSum = function(nums, target) {
    for (const [index, num] of nums.entries()) {
        const find_num = target - num;

        let find_result = nums.slice(index + 1).findIndex(n => n === find_num);
        if (find_result !== -1) {
            let found_index = find_result + index + 1;
            let result = [index, found_index];
            return result;
        }
    }
};
PreviousJavaNextKotlin

Last updated 5 years ago

Was this helpful?