当前位置:网站首页>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
边栏推荐
- [Redis]-[Redis底层数据结构]-字典
- Learning Tai Chi maker esp8226 (IX) JSON data communication III
- 2022-2028 global section valve industry research and trend analysis report
- [DB written interview 225] in Oracle, if the online redo log file is damaged, how to recover it?
- Using elastic stack to analyze Olympic data (II)
- Qunhui dsm7 add kit source
- 2021-06-18 STM32F103 DMA and DMA serial port code using firmware library
- Global and Chinese market for online automatic optical inspection 2022-2028: Research Report on technology, participants, trends, market size and share
- 1004 counting leaves (30 points)
- Scientific research information | national natural conclusion regulations: more than 50% of the fund balance or it will not be concluded
猜你喜欢

为什呢代码没报错但是数据库里边的数据显示不出来

Fd: file descriptor

【元宇宙3d大赛】

Weekly update | showmebug officially launched Tencent conference audio and video

2022-2028 global internal gear motor industry research and trend analysis report

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

Linux安装达梦数据库/DM8(附带客户端工具安装完整版)

Diary (C language summary)

Bean实例化的三种方法

Linux Installation of Damon database /dm8 (with client tool installation full version)
随机推荐
Three methods of bean instantiation
For hand drawn graphics, covering multiple topics, CVPR 2022 sketchdl workshop begins to solicit contributions!
【元宇宙3d大赛】
Inline functions in kotlin
Markdown rule for writing articles
Le Code est correct, mais les données de la base de données ne sont pas affichées.
结构体类型的三种声明方式
Bean实例化的三种方法
2022-2028 global boom cylinder industry research and trend analysis report
2022-2028 global internal gear motor industry research and trend analysis report
Antd table how scroll bars appear in long tables
古风排版 (20 分)(测试点4)
MySQL filter query (start with a letter, start with a number, start without a number, and start without a letter)
Construct URL and Base64 download in the form of binary stream for file download
LeetCode数组经典题目(一)
[kotlin] premier jour
函数声明和函数表达式的区别
Global and Chinese market of Toro from 2022 to 2028: Research Report on technology, participants, trends, market size and share
2021-07-28 STM32F103 configuration information
8 methods of getting user ID in WordPress