当前位置:网站首页>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;
}
返回回去的变量一定要动态开辟内存,因为局部变量保存在栈上,随着函数的生命周期的结束会内存销毁,而动态开辟的内存是在堆上,函数结束,并不会释放该内存。
边栏推荐
- d,ldc构建共享库
- Kivy教程大全之如何在 Kivy 中创建下拉列表
- R language generalized linear model function GLM, (model fit and expression diagnostics), model adequacy evaluation method, use plot function and car package function
- 软考信息系统项目管理师_历年真题_2019下半年错题集_上午综合知识题---软考高级之信息系统项目管理师053
- 【第29天】给定一个整数,请你求出它的因子数
- Create your first Kivy program Hello word (tutorial includes source code)
- Embrace the safety concept of platform delivery
- 按键精灵打怪学习-前台和内网发送后台验证码
- [my advanced journey of OpenGL learning] collation of Euler angle, rotation order, rotation matrix, quaternion and other knowledge
- Is there anything in common between spot gold and spot silver
猜你喜欢
异步、郵件、定時三大任務
[FPGA tutorial case 6] design and implementation of dual port RAM based on vivado core
机器学习术语
Excel calculates the difference between time and date and converts it into minutes
C application interface development foundation - form control (2) - MDI form
【FH-GFSK】FH-GFSK信号分析与盲解调研究
JDBC courses
Database SQL language 01 where condition
Excel if formula determines whether the two columns are the same
What is tone. Diao's story
随机推荐
[FPGA tutorial case 6] design and implementation of dual port RAM based on vivado core
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
Esp32 simple speed message test of ros2 (limit frequency)
[Arduino experiment 17 L298N motor drive module]
Mathematical knowledge: step Nim game game game theory
【C语言】指针与数组笔试题详解
按键精灵打怪学习-自动寻路回打怪点
uniapp组件-uni-notice-bar通告栏
Matlab Doppler effect produces vibration signal and processing
What is tone. Diao's story
Mathematical Knowledge: Steps - Nim Games - Game Theory
SwiftUI 组件大全之使用 SceneKit 和 SwiftUI 构建交互式 3D 饼图(教程含源码)
[untitled]
How wide does the dual inline for bread board need?
Draw love with go+ to express love to her beloved
數學知識:臺階-Nim遊戲—博弈論
Canvas drawing -- bingdd
数学知识:台阶-Nim游戏—博弈论
tail -f 、tail -F、tailf的区别
Machine learning terminology