当前位置:网站首页>Leedcode 1 - sum of two numbers
Leedcode 1 - sum of two numbers
2022-06-21 08:15:00 【lqonlylove】
https://leetcode-cn.com/problems/two-sum/
One 、 subject
Given a An array of integers nums And a Integer target target, Please enter your name in the array find And is the target value target the Two integers , and return Their The array subscript .
You can Suppose that each input corresponds to only one answer . however , The same element in the array cannot be repeated in the answer .
You can return the answers in any order .
Two 、 Function description
**/** * Parameters : * nums: Enter the first address of the array ( Input ) * numsSize: Enter the array length ( Input ) * target: Sum of two integers ( Input ) * returnSize: Return data length ( Output ) * Return value : preservation nums Array 2 The sum of the values equals target Data 2 An array of subscripts */
/** * Note: The returned array must be malloced, assume caller calls free(). */
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
}**
3、 ... and 、 Example
Example 1 :
Input :nums = [2,7,11,15], target = 9
Output :[0,1]
explain : because nums[0] + nums[1] == 9 , return [0, 1] .
Example 2 :
Input :nums = [3,2,4], target = 6
Output :[1,2]
Example 3 :
Input :nums = [3,3], target = 6
Output :[0,1]
Four 、 Advanced
You can come up with a time complexity less than O(n*n) The algorithm of ?
5、 ... and 、 Write answers
1、 Ideas
Index from the array 0 Start , To numsSize -1 Enumerate data . For each nums[i] lookup target - x Whether there is .
2、 Code
/** * Note: The returned array must be malloced, assume caller calls free(). */
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
int *result = NULL;
int i = 0,j=0;
for(i=0;i<numsSize-1;i++){
for(j=i+1;j<numsSize;j++){
if(nums[i]+nums[j] == target){
result = (int*)malloc(sizeof(int)*2);
result[0] = i,result[1] = j;
*returnSize = 2;
return result;
}
}
}
*returnSize = 0;
return result;
}
3、 analysis
Time complexity :O(N*N), among N Is in the array Element quantity . The worst Any two numbers in the next array must be matched once .
Spatial complexity :O(1).
5、 ... and 、 All solutions
1、 The first one is : Violence enumeration
Their thinking : The easiest way to think of it is enumeration Every number in the array x, seek Whether there is... In the array target - x. Enumerating data There will be a cycle ( Time complexity O(N)), lookup target - x A loop will be nested ( Time complexity O(N)), So the total time complexity is O(N*N).
2、 The second kind : Hashtable
Their thinking : adopt Hashtable Complete the data enumeration , Proceed again seek target - x Whether there is . Use Hashtable , You can look for target - x The time complexity is reduced to from O(N) Down to O(1), lookup target - x A loop will be nested ( Time complexity O(N)), So the total time complexity is O(N).
6、 ... and 、 Additional explanation
1、 Hashtable
https://cloud.tencent.com/developer/article/1781904
https://blog.csdn.net/weixin_34153142/article/details/104867504
https://blog.csdn.net/qq_39630587/article/details/77196367
https://zhuanlan.zhihu.com/p/144296454
2、 Prime number ( prime number )
https://baike.baidu.com/item/%E8%B4%A8%E6%95%B0/263515
边栏推荐
- There was a GC failure in the online go service. I was in a hurry
- 图解 Google V8 # 16:V8是怎么通过内联缓存来提升函数执行效率的?
- PS prompts "script error -50 general Photoshop error, how to solve it?
- [kotlin] first day
- Scientific research information | national natural conclusion regulations: more than 50% of the fund balance or it will not be concluded
- How to write attractive titles for short videos? Learn these tips to make your title more convincing
- Showctf web primer series
- An aunt's towel holds up the 100 billion market behind 400million Chinese women
- 2022-2028 global cooling on-off valve industry research and trend analysis report
- Eureka的TimedSupervisorTask类(自动调节间隔的周期性任务)
猜你喜欢

5 minutes to understand MySQL - row to column

Figure neural network and cognitive reasoning - Tang Jie - Tsinghua University

日記(C語言總結)

Listing of flaunting shares on Shenzhen Stock Exchange: market value of 4.2 billion, 9-month revenue decreased by 21% year-on-year

Decrypt FTP

How can we make millions a year now?

2022-2028 global hydrogen engine industry research and trend analysis report

Illustration of Google V8 16: how does V8 improve function execution efficiency through inline caching?

Cluster hui dsm7 add suite source

There was a GC failure in the online go service. I was in a hurry
随机推荐
Global and Chinese market of horizontal drilling rigs 2022-2028: Research Report on technology, participants, trends, market size and share
线上GO服务出现GC故障,我当时就急了
[DB written interview 366] stored procedures are codes stored in the database and have many advantages. Among the following statements, which are not the advantages of stored procedures are ()
2022-2028 global internal gear motor industry research and trend analysis report
Practical application cases of digital Twins - coal mine
请问这些要求用mysql能实现吗
Global and Chinese market for packed gas chromatographic columns 2022-2028: Research Report on technology, participants, trends, market size and share
5 minutes to understand MySQL - row to column
Summary of command execution knowledge points in CTF
Leetcode topic [array] -40- combined sum II
Linux Installation of Damon database /dm8 (with client tool installation full version)
Global and Chinese market of electrical connectors 2022-2028: Research Report on technology, participants, trends, market size and share
图解 Google V8 # 14:字节码(二):解释器是如何解释执行字节码的?
Le Code est correct, mais les données de la base de données ne sont pas affichées.
FD:文件描述符
Cobaltstrike office macro virus utilization
利用注解改进代码检查
Markdown rule for writing articles
复数四则运算(二十三行简洁代码)
Multiplication and addition of univariate polynomial (20 points)