当前位置:网站首页>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 .
边栏推荐
- CAS比较交换的知识、ABA问题、锁升级的流程
- Spark动态资源分配的资源释放过程及BlockManager清理过程
- Hyperlink parsing in MD: parsing `this$ Set() `, ` $` should be preceded by a space or escape character`\`
- Text batch replacement function
- Analysis of spark task scheduling exceptions
- UDP 的报文结构和注意事项
- 股票开户佣金优惠,炒股开户哪家证券公司好网上开户安全吗
- [Yunxiang book club issue 13] common methods of viewing media information and processing audio and video files in ffmpeg
- Fluent -- layout principle and constraints
- go语言慢速入门——go运算符
猜你喜欢
随机推荐
multimap案例
Use deconstruction to exchange the values of two variables
Hyperlink parsing in MD: parsing `this$ Set() `, ` $` should be preceded by a space or escape character`\`
折半插入排序
Troubleshooting the slow startup of spark local programs
【剑指offer】面试题54:二叉搜索树的第k大节点
Spark RPC
js寻找数组中的最大和最小值(Math.max()方法)
What format is this data returned from the background
【剑指offer】面试题49:丑数
Singles cup, web:web check in
Spark 3.0 adaptive execution code implementation and data skew optimization
【剑指offer】面试题39:数组中出现次数超过一半的数字
Binary Insertion Sort
Spark 3.0 testing and use
Spark TroubleShooting整理
The difference between synchronized and reentrantlock
Database: use the where statement to retrieve (header song)
Spark 3.0 DPP implementation logic
《吐血整理》C#一些常用的帮助类








![[0 basic operations research] [super detail] column generation](/img/cd/f2521824c9ef6a50ec2be307c584ca.png)
