当前位置:网站首页>Leetcode-1: sum of two numbers
Leetcode-1: sum of two numbers
2022-07-27 15:45:00 【FA FA is a silly goose】
subject : Given an array of integers nums And an integer target value target, Please find... In the array And is the target value target the Two Integers , And return their array subscripts .
You can assume 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 .
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]
Tips :
2 <= nums.length <= 104-109 <= nums[i] <= 109-109 <= target <= 109- There will only be one valid answer
Advanced : You can come up with a time complexity less than O(n2) The algorithm of ?
Code :
/** * Note: The returned array must be malloced, assume caller calls free(). */
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
for (int i = 0; i < numsSize; i++) {
for (int j = i + 1; j < numsSize; j++) {
if (nums[i] + nums[j] == target) {
int *ret =(int*)malloc(sizeof(int) * 2);
ret[0] = i;
ret[1] = j;
*returnSize = 2;
return ret;
}
}
}
*returnSize = 0;
return NULL;
}
analysis :1. Function return type is int*, From the prompt , It returns an array , The parameters in the function are the address of the first element of the input array 、 Enter the number of elements in the array 、 The target 、 Returns the number of array elements ( The number of returned array elements is int * type , because int Type cannot be taken back ).
2. Use two layers for Loop to complete the sum of any two numbers in the array and compare with the target value , Open up space in the cycle , Used to store the corresponding array subscript .
边栏推荐
- JS uses for in and for of to simplify ordinary for loops
- 线程间等待与唤醒机制、单例模式、阻塞队列、定时器
- JS uses extension operators (...) to simplify code and simplify array merging
- Spark Filter算子在Parquet文件上的下推
- 【剑指offer】面试题53-Ⅰ:在排序数组中查找数字1 —— 二分查找的三个模版
- 【剑指offer】面试题54:二叉搜索树的第k大节点
- Implement custom spark optimization rules
- 股票开户佣金优惠,炒股开户哪家证券公司好网上开户安全吗
- Go language slow start -- go operator
- Hyperlink parsing in MD: parsing `this$ Set() `, ` $` should be preceded by a space or escape character`\`
猜你喜欢

Half find

语音直播系统——提升云存储安全性的必要手段

Troubleshooting the slow startup of spark local programs

【剑指offer】面试题46:把数字翻译成字符串——动态规划

/dev/loop1占用100%问题

IP protocol of network layer

Binder initialization process

Spark 3.0 Adaptive Execution 代码实现及数据倾斜优化

QT (IV) mixed development using code and UI files

Spark TroubleShooting整理
随机推荐
Voice live broadcast system -- a necessary means to improve the security of cloud storage
Troubleshooting the slow startup of spark local programs
Explanation of various attributes of "router link"
/dev/loop1占用100%问题
Spark 本地程序启动缓慢问题排查
Multi table query_ Sub query overview and multi table query_ Sub query situation 1 & situation 2 & situation 3
使用Prometheus监控Spark任务
C语言:字符串函数与内存函数
股票开户佣金优惠,炒股开户哪家证券公司好网上开户安全吗
Read the wheelevent in one article
What format is this data returned from the background
Spark 任务Task调度异常分析
Is low code the future of development? On low code platform
【剑指offer】面试题42:连续子数组的最大和——附0x80000000与INT_MIN
[正则表达式] 单个字符匹配
实体类(VO,DO,DTO)的划分
聊聊ThreadLocal
初识结构体
Alibaba's latest summary 2022 big factory interview real questions + comprehensive coverage of core knowledge points + detailed answers
Spark Bucket Table Join