Live the ordinary life well

It was the most extraordinary thing of all

preface

A nervous witness is being questioned by a lawyer.

“Have you ever been married?” the lawyer asked sharply.

“Yes, I did once.”

The witness was quiet and trembling.

“So who did you marry?”

“A woman.”

The lawyer was angry

“Nonsense, of course you’re married to a woman. Have you ever heard of anyone marrying a man? ‘

The witness shuddered and said, “Yes, my sister married a man.”

A person is incomplete, two people have no regrets with what! You are a person to come, how can not a person go a person see lonely, two people see fireworks I just don’t care about you, anyway, the last no matter a person or two people, you will regret ╥﹏╥\

1 Analysis of Ideas

Input: nums = [2,7,11,15], target = 9

1.1 Basic Steps

  1. Iterate over nums to get num and I
  2. Find the position j of 9-num
  3. Return to [j] I

1.2 Key Steps

Find the position j of 9-num

Create hash mapping where key = num and value = I 2.2. Find the hash table and get the value

2 Code snippet

/** * @param {number[]} nums * @param {number} target * @return {number[]} */ var twoSum = function (nums, Has, get, set const m = new map () // 1 Nums for (let I = 0; i < nums.length; i++) { const num = nums[i] // 2. If (m.trap (target-num)) {const j = m.trap (target-num) // 3 Return [j, I]} else {// 2.1 create hash map // avoid adding the same element [3, 2, 4] 6 m.et (num, I)}}}Copy the code

3 Skills Summary

1.Map

// o = {}
m = new Map()

// o.a = b
m.set('a', b)

// o.a
m.get('a')

// a in o
m.has('a')

// delete o.a
m.delete('a')

// Object.keys(o).length
m.size
Copy the code

2. Two-layer for loop deconstruction

// for * for for (let i=0; i<nums.length; i++) { for (let j=0; j<nums.length; J ++) {if (nums[I] + nums[j] === target) {return [I, j]}}} // for + for for (let j=0; j<nums.length; J ++) {m.et (nums[j], j)} // loop for (let I =0; i<nums.length; i++) { if (m.has(target - nums[i])) { return [i, m.get(target - nums[i])] } } // for for (let i=0; i<nums.length; If (m.haas (target-nums [I])) {return [m.net (target-nums [I]), I]} else { Nums [I], I}}Copy the code





Who would marry a man



Not a man, of course

Refer to the link

  • The power button’s official website
  • The sum of two Numbers

Review past

  • Look forward to the next period (*❦ω❦)