How to happily brush LeetCode with Vscode

After leetcode has a Chinese site, it becomes more convenient to swipe questions. However, instead of using an editor given on a web page to solve the problem, they prefer to use a common editor like vscode to solve the problem.

There is a plugin on vscode that allows you to easily use your own account to brush LeetCode titles on vscode. The feature of this plugin is:

  • Log in/out of LeetCode account
  • Switch between the international station and the Chinese station
  • Select topics by category (difficulty, company, label)
  • Editor shortcuts for the topic discussion area and solutions
  • Keyword search topic

The sum of two Numbers

The subject content

Given an array of integers nums and a target value target, find the two integers in the array and the target values and return their array subscripts.

Solution one, brute force

After reading the problem for the first time, it is quite natural to think of using a double cycle to calculate direct violence. That is, add them one by one to determine whether they are equal to the target value, and if so, return the subscript of the current number of cycles. But there is no doubt that time complexity will be high.

In the process of brute force cracking, I encountered a problem. At the beginning, I directly used two forEach, but I could not pass Submit all the time. There is no other way to terminate the forEach loop than to throw an exception, so the item inside the return will not be returned.

var twoSum = function (nums, target) {
    for (let i = 0; i < nums.length; i++) {
        for (let j = i + 1; j < nums.length; j++) {
            if (nums[i] + nums[j] === target) {
                return [i, j]
            }
        }
    }
    // nums.forEach((e, i) => {
    //      nums.forEach((m, n) => {
    //          if (e + m === target) {
    //              return[I, n] //} //}) //}) // There is no way to abort or jump out except by throwing an exceptionforEach () loop. // If you need to break out of the loop,forThe Each() method is not the tool you should use. // Therefore, the above code is useless. };Copy the code

The second solution is to take it by object

Given the target value, there must be two valid target values in a given array. Calculate the difference between the target value and the current value of the loop, assuming that all values in the loop are likely to be the answer. And the coordinates of the current value are saved in the form of an object with the value as key and the table below as value. In a later loop an RES will always be found in obj. At this point, the following table in OBj and the subscript of the current loop are the answers.

const twoSum = (nums, target) => {
const obj = {}
for (let i = 0, len = nums.length; i < len; ++i) {
        const data = nums[i]
        const res = target - data
        if (res in obj) {
            return [obj[res], i]
        }
        obj[data] = i
        console.log(i)
    }
}
twoSum([7, 2, 11, 16], 9)
Copy the code