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

Example Analysis of the Sum of two numbers in LeetCode

2025-04-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

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!

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