Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

LeetCode realizes the Sum of two numbers

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report