当前位置:网站首页>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;
}
返回回去的变量一定要动态开辟内存,因为局部变量保存在栈上,随着函数的生命周期的结束会内存销毁,而动态开辟的内存是在堆上,函数结束,并不会释放该内存。
边栏推荐
- Assets, vulnerabilities, threats and events of the four elements of safe operation
- Kivy教程大全之如何在 Kivy 中创建下拉列表
- 1696C. Fishingprince plays with array [thinking questions + intermediate state + optimized storage]
- C#应用程序界面开发基础——窗体控制(2)——MDI窗体
- Mathematical knowledge: step Nim game game game theory
- leetcode 6103 — 从树中删除边的最小分数
- The latest analysis of tool fitter (technician) in 2022 and the test questions and analysis of tool fitter (technician)
- 对非ts/js文件模块进行类型扩充
- 【FPGA教程案例5】基于vivado核的ROM设计与实现
- d,ldc构建共享库
猜你喜欢

【面试题】1369- 什么时候不能使用箭头函数?
![[flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)](/img/ca/1d2473ae51c59b84864352eb17de94.jpg)
[flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)

MySQL基础用法02

异步、邮件、定时三大任务

什么是调。调的故事

Arduino dy-sv17f automatic voice broadcast

Arduino DY-SV17F自动语音播报

机器学习术语

【FH-GFSK】FH-GFSK信号分析与盲解调研究

Soft exam information system project manager_ Real topic over the years_ Wrong question set in the second half of 2019_ Morning comprehensive knowledge question - Senior Information System Project Man
随机推荐
Create your first Kivy program Hello word (tutorial includes source code)
Button wizard play strange learning - automatic return to the city route judgment
产业互联网的产业范畴足够大 消费互联网时代仅是一个局限在互联网行业的存在
Detailed explanation of Q-learning examples of reinforcement learning
2022 coal mine gas drainage examination question bank and coal mine gas drainage examination questions and analysis
The difference between tail -f, tail -f and tail
Draw love with go+ to express love to her beloved
Kivy tutorial how to create drop-down lists in Kivy
按键精灵打怪学习-自动回城路线的判断
[shutter] animation animation (the core class of shutter animation | animation | curvedanimation | animationcontroller | tween)
How is the mask effect achieved in the LPL ban/pick selection stage?
2022 Jiangxi Provincial Safety Officer B certificate reexamination examination and Jiangxi Provincial Safety Officer B certificate simulation examination question bank
leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]
Makefile中wildcard、patsubst、notdir的含义
leetcode 2097 — 合法重新排列数对
Why can't the start method be called repeatedly? But the run method can?
C#应用程序界面开发基础——窗体控制(1)——Form窗体
不登陆或者登录解决oracle数据库账号被锁定。
Swiftui component Encyclopedia: using scenekit and swiftui to build interactive 3D pie charts (tutorial with source code)
异步、邮件、定时三大任务