Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Example Analysis of the Sum of two numbers in LeetCode

Shulou Source: shulou.com Published: 2022-06-01 13:11:51 09月10日 Update

Editor to share with you the example analysis of the sum of two numbers in LeetCode. I hope you will get something after reading this article. Let's discuss it together.

The sum of two numbers

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

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

Return [0,1] because nums [0] + nums [1] = 2 + 7 = 9

The code is as follows:

Class Solution {public: vector twoSum (vector& nums, int target) {vector ret; int size = nums.size (); int * num = new int [nums.size ()]; memcpy (num, & nums [0], nums.size () * sizeof (int)); for (int I = 0; I < size;i++) {for (int j = I + 1; j < size) Ret.push_back +) {if (Num [I] + Num [j] = = target) {ret.push_back (I); ret.push_back (j);} delete [] num; return ret;}} After reading this article, I believe you have some understanding of "sample Analysis of the Sum of two numbers in LeetCode". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for your reading!

Tags: Sum examples arrays analysis integers goals target values articles two code elements over more knowledge answers industry information information channels channels input Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS OPPO Reno Shulou Tech Info Shulou Technology Docker