当前位置:网站首页>[C题目]力扣1. 两数之和
[C题目]力扣1. 两数之和
2022-08-02 20:33:00 【GLC8866】
思路: 枚举所有的元素组合,满足和为target时,将两个元素下标赋值到数组nums的第1和第2个位置,修改数组的长度为2,并且返回数组。
注意:这里虽然用动态申请空间也能通过,但是调函该函数的人可能不知道你动态申请了空间,如果多次调用又不释放,就会导致内存泄漏。出题者给出了*returnSize的目的就是想让你利用数组nums作为"容器"储存两个元素的下标传递出来,而之所以用指针的形式是因为只有通过指针才能改变函数外的数组的长度(变量)。
int* twoSum(int* nums, int numsSize, int target, int* returnSize)
{
for(int i=0;i<numsSize;i++)//nums[i]和它后面的所有元素一对对组合,看是否和为target。
{
for(int j=i+1;j<numsSize;j++)
{
if(nums[i]+nums[j]==target)//满足要求就把下标赋值给数组num前两位,数组nums作为传递数据的"容器"。
{
nums[0]=i;
nums[1]=j;
*returnSize=2;//改变数组的长度,用指针才能改变函数之外的变量。
return nums;
}
}
}
return nums;
}
边栏推荐
- How to use windbg check c # a thread stack size?
- iframe------------frame-
- 软件成分分析:华为云重磅发布开源软件治理服务
- Geoip2 - golang golang source code analysis
- Helm基础知识
- ORB SLAM3加载Vocabulary更快ORBvoc.bin
- .NET如何快速比较两个byte数组是否相等
- 回文自动机+CodeTON Round 2 C,D
- Tencent YunMeng every jie: I experienced by cloud native authors efficiency best practices case
- Li Mu hands-on deep learning V2-BERT pre-training and code implementation
猜你喜欢

二叉搜索树的实现

李沐动手学深度学习V2-BERT预训练和代码实现

.NET performance optimization - you should set initial size for collection types
人尽皆知的云原生,到底是大势所趋还是过度炒作?

Use the TCP protocol, we won't lost package?

软件成分分析:华为云重磅发布开源软件治理服务
![Informatics Olympiad All-in-One (1259: [Example 9.3] Find the longest non-descending sequence)](/img/a2/6d548909341a65129db2e69b90e5bf.png)
Informatics Olympiad All-in-One (1259: [Example 9.3] Find the longest non-descending sequence)

接口测试常用工具及测试方法(入门篇)

iframe------------frame-

美国爱荷华州立大学| Improving Distantly Supervised Relation Extraction by Natural Language Inference(通过自然语言推理改进远程监督关系提取)
随机推荐
PLC工作原理动画
golang源码分析:time/rate
Use the TCP protocol, we won't lost package?
js how to get the browser zoom ratio
A brief discussion on the transformation of .NET legacy applications
解道7-编程技术4
Details in C# you don't know
2170. 使数组变成交替数组的最少操作数
Bena的生命周期
用户之声 | 我与GBase的缘分
Tencent YunMeng every jie: I experienced by cloud native authors efficiency best practices case
golang source code analysis: uber-go/ratelimit
DataGrip 安装教程 详细版
y85.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶、pushgateway和prometheus存储(十六)
性能测试 - 理论
Wiring diagrams of switches, motors, circuit breakers, thermocouples, and meters
二叉搜索树的实现
用了TCP协议,就一定不会丢包吗?
Linphone 被叫方如何解析来电SIP消息中的自定义头消息
ABAP grammar small review