当前位置:网站首页>leetcode刷题_两数之和 II - 输入有序数组
leetcode刷题_两数之和 II - 输入有序数组
2022-07-03 01:05:00 【身影王座】
题目描述

java解决方法
class Solution {
public int[] twoSum(int[] numbers, int target) {
int index1 = 0;
int index2 = numbers.length - 1;
int[] index = new int[2];
while(index1 < index2)
{
if(numbers[index2] + numbers[index1] > target)
{
index2--;
}
else if(numbers[index2] + numbers[index1] < target)
{
index1++;
}
else
{
index[0] = index1 + 1;
index[1] = index2 + 1;
break;
}
}
return index;
}
}
C解决方法
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* numbers, int numbersSize, int target, int* returnSize){
int *index = NULL;
int index1 = 0;
int index2 = numbersSize - 1;
index = (int*)malloc(2 * sizeof(int));
while(index1 < index2)
{
if(numbers[index1] + numbers[index2] > target)
{
index2--;
}
else if(numbers[index1] + numbers[index2] < target)
{
index1++;
}
else
{
index[0] = index1 + 1;
index[1] = index2 + 1;
break;
}
}
*returnSize = 2;
return index;
}
返回回去的变量一定要动态开辟内存,因为局部变量保存在栈上,随着函数的生命周期的结束会内存销毁,而动态开辟的内存是在堆上,函数结束,并不会释放该内存。
边栏推荐
- Trois tâches principales: asynchrone, courrier et timing
- [Cao gongzatan] after working in goose factory for a year in 2021, some of my insights
- Kivy tutorial - example of using Matplotlib in Kivy app
- Detailed explanation of Q-learning examples of reinforcement learning
- Kivy教程大全之如何在 Kivy 中创建下拉列表
- SwiftUI 组件大全之使用 SceneKit 和 SwiftUI 构建交互式 3D 饼图(教程含源码)
- 2022 Jiangxi Provincial Safety Officer B certificate reexamination examination and Jiangxi Provincial Safety Officer B certificate simulation examination question bank
- 音程的知识的总结
- What are the trading forms of spot gold and what are the profitable advantages?
- kivy教程之在 Kivy App 中使用 matplotlib 的示例
猜你喜欢

Machine learning terminology

Using tensorboard to visualize the model, data and training process
![[my advanced journey of OpenGL learning] collation of Euler angle, rotation order, rotation matrix, quaternion and other knowledge](/img/ed/23331d939c9338760e426d368bfd5f.png)
[my advanced journey of OpenGL learning] collation of Euler angle, rotation order, rotation matrix, quaternion and other knowledge

MySQL

MySQL - database query - condition query

MySQL basics 03 introduction to MySQL types

一比特苦逼程序員的找工作經曆

Trois tâches principales: asynchrone, courrier et timing

Meituan dynamic thread pool practice ideas, open source

Arduino dy-sv17f automatic voice broadcast
随机推荐
Telephone network problems
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
d. LDC build shared library
Excel removes the data after the decimal point and rounds the number
MySQL - database query - condition query
【面试题】1369- 什么时候不能使用箭头函数?
2022 Jiangxi Provincial Safety Officer B certificate reexamination examination and Jiangxi Provincial Safety Officer B certificate simulation examination question bank
How wide does the dual inline for bread board need?
按键精灵打怪学习-自动回城路线的判断
[system analyst's road] Chapter V double disk software engineering (development model development method)
Look at how clothing enterprises take advantage of the epidemic
Assets, vulnerabilities, threats and events of the four elements of safe operation
看疫情之下服装企业如何顺势而为
Vim 9.0正式发布!新版脚本执行速度最高提升100倍
JS inheritance and prototype chain
MySQL foundation 06 DDL
Swiftui component Encyclopedia: using scenekit and swiftui to build interactive 3D pie charts (tutorial with source code)
MySQL foundation 05 DML language
Mongodb common commands of mongodb series
R language uses coin package to apply permutation tests to independence problems (permutation tests, whether response variables are independent of groups, are two numerical variables independent, and