当前位置:网站首页>leetcode-1:两数之和
leetcode-1:两数之和
2022-07-27 14:27:00 【发发是只呆头鹅】
题目:给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
你可以按任意顺序返回答案。
示例1:
输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
示例2:
输入:nums = [3,2,4], target = 6
输出:[1,2]
示例3:
输入:nums = [3,3], target = 6 输出:[0,1]
提示:
2 <= nums.length <= 104-109 <= nums[i] <= 109-109 <= target <= 109- 只会存在一个有效答案
进阶:你可以想出一个时间复杂度小于 O(n2) 的算法吗?
代码:
/** * 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;
}
分析:1.函数返回类型为int*,从提示中可知,返回的是一个数组,函数中的参数分别为输入数组的首元素地址、输入数组中元素个数、目标值、返回数组元素的个数(返回数组元素个数为int * 类型,因为int类型带不回去)。
2.用两层for循环来完成数组中的任意两数相加之和与目标值比较,在循环中开辟空间,用来存放对应的数组下标。
边栏推荐
- STM32F10x_ Hardware I2C read / write EEPROM (standard peripheral library version)
- [正则表达式] 匹配分组
- Multi table query_ Sub query overview and multi table query_ Sub query situation 1 & situation 2 & situation 3
- Network equipment hard core technology insider router Chapter 10 Cisco asr9900 disassembly (III)
- Deveco studio2.1 operation item error
- Spark TroubleShooting整理
- Network equipment hard core technology insider router Chapter 18 dpdk and its prequel (III)
- Network equipment hard core technology insider router Chapter 16 dpdk and its prequel (I)
- EMC design scheme of RS485 interface
- 修改 Spark 支持远程访问OSS文件
猜你喜欢

Four kinds of relay schemes driven by single chip microcomputer

Tools - common methods of markdown editor

Unity performance optimization ----- occlusion culling of rendering optimization (GPU)

实现自定义Spark优化规则

EMC design scheme of USB2.0 Interface

Adaptation verification new occupation is coming! Huayun data participated in the preparation of the national vocational skill standard for information system adaptation verifiers

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

Method of removing top navigation bar in Huawei Hongmeng simulator

Photoelectric isolation circuit design scheme (six photoelectric isolation circuit diagrams based on optocoupler and ad210an)

EMC design scheme of RS485 interface
随机推荐
Multi table query_ Sub query overview and multi table query_ Sub query situation 1 & situation 2 & situation 3
Spark 任务Task调度异常分析
Complexity analysis
股票开户佣金优惠,炒股开户哪家证券公司好网上开户安全吗
Photoelectric isolation circuit design scheme (six photoelectric isolation circuit diagrams based on optocoupler and ad210an)
Leetcode 190. reverse binary bit operation /easy
USB interface electromagnetic compatibility (EMC) solution
【剑指offer】面试题39:数组中出现次数超过一半的数字
Tools - common methods of markdown editor
shell脚本读取文本中的redis命令批量插入redis
西瓜书《机器学习》阅读笔记之第一章绪论
How to package AssetBundle
设置提示框位置随鼠标移动,并解决提示框显示不全的问题
js运用扩展操作符(…)简化代码,简化数组合并
How to take satisfactory photos / videos from hololens
使用双星号代替Math.pow()
Spark 3.0 Adaptive Execution 代码实现及数据倾斜优化
Network equipment hard core technology insider router Chapter 11 Cisco asr9900 disassembly (V)
Database: use the where statement to retrieve (header song)
实现自定义Spark优化规则