Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

LeetCode realizes the Sum of two numbers

Shulou Source: shulou.com Published: 2022-06-02 10:08:33 09月12日 Update

This article introduces the relevant knowledge of "LeetCode realizes the sum of two numbers". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Topic description

Given an array of integers nums and a target value target, please find the two integers in the array that are the target values and return their array subscript.

You can assume that each input corresponds to only one answer. However, you cannot reuse the same elements in this array.

Example:

Given nums = [2,7,11,15], target = 9 returns [0,1] because nums [0] + nums [1] = 2 + 7 = 9

Solution-the idea of solving problems by violent solution

The easiest thing to think of is the violence method, which uses two iterations to find out whether the sum of the two numbers is target. The process is as follows:

Use I to traverse each element of the array

In each loop of I, use j to traverse from I + 1

Judge whether the sum of the values corresponding to I and j is target

Code implementation func twoSum (nums [] int, target int) [] int {for I: = 0; I < len (nums); I + + {for j: = I + 1; j < len (nums); j + + {if nums [I] + nums [j] = = target {return [] int {ijue j} return [] int {} complexity analysis

Time complexity: O (n ^ 2). There are two loops, and the event complexity of each loop is O (n). The total complexity is O (n ^ 2).

Space complexity: O (1).

The idea of solving problems by two-hash table method

A hash table is used in this solution. When you traverse the array, the current element is added to the hash table. It also checks whether the target element corresponding to the current element already exists in the hash table. Return if it exists.

Code implementation func twoSum2 (nums [] int, target int) [] int {m: = make (map [int]) for I: = 0; I < len (nums); I + + {if index, exists: = m [target-nums [I]]; exists {return [] int {iMagin index}} m [nums [I]] = I} return [] int {} complexity analysis

Time complexity: O (n). We only iterated through the array once.

Space complexity: O (n). Hash tables are used and extra space is needed. Depending on the number of elements stored in the hash table, the table needs to store up to O (n) elements.

This is the end of the content of "LeetCode realizes the sum of two numbers". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Complexity elements arrays hashes solutions goals spaces loops two code content ideas integers time violence more target values knowledge analysis Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Microsoft MySQL Xiaomi Docker