当前位置:网站首页>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;
}
返回回去的变量一定要动态开辟内存,因为局部变量保存在栈上,随着函数的生命周期的结束会内存销毁,而动态开辟的内存是在堆上,函数结束,并不会释放该内存。
边栏推荐
- MySQL basic usage 02
- Daily topic: movement of haystack
- 异步、邮件、定时三大任务
- 数学知识:Nim游戏—博弈论
- Excel if formula determines whether the two columns are the same
- The latest analysis of tool fitter (technician) in 2022 and the test questions and analysis of tool fitter (technician)
- 按键精灵打怪学习-自动回城路线的判断
- Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
- Strongly connected components of digraph
- Mathematical Knowledge: Steps - Nim Games - Game Theory
猜你喜欢

攻克哈希的基本概念与实现

Niu Ke swipes questions and clocks in

每日一题之干草堆的移动

Database SQL language 02 connection query

Arduino DY-SV17F自动语音播报

什么是调。调的故事
![[Arduino experiment 17 L298N motor drive module]](/img/e2/4511eaa942e4a64c8ca2ee70162785.jpg)
[Arduino experiment 17 L298N motor drive module]

一位苦逼程序员的找工作经历

Matlab Doppler effect produces vibration signal and processing
![[C language] detailed explanation of pointer and array written test questions](/img/24/c2c372b5c435cbd6eb83ac34b68034.png)
[C language] detailed explanation of pointer and array written test questions
随机推荐
力扣 204. 计数质数
按键精灵打怪学习-回城买药加血
kivy教程之在 Kivy App 中使用 matplotlib 的示例
C#应用程序界面开发基础——窗体控制(2)——MDI窗体
[Cao gongzatan] after working in goose factory for a year in 2021, some of my insights
C application interface development foundation - form control (2) - MDI form
Vim 9.0正式发布!新版脚本执行速度最高提升100倍
Androd gradle's substitution of its use module dependency
一位苦逼程序员的找工作经历
SwiftUI 组件大全之使用 SceneKit 和 SwiftUI 构建交互式 3D 饼图(教程含源码)
How wide does the dual inline for bread board need?
Create your first Kivy program Hello word (tutorial includes source code)
18_ The wechat video number of wechat applet scrolls and automatically plays the video effect to achieve 2.0
Database SQL language 02 connection query
數學知識:臺階-Nim遊戲—博弈論
2022 cable crane driver examination registration and cable crane driver certificate examination
d,ldc構建共享庫
对非ts/js文件模块进行类型扩充
【第29天】给定一个整数,请你求出它的因子数
Give you an array numbers that may have duplicate element values. It was originally an array arranged in ascending order, and it was rotated once according to the above situation. Please return the sm