当前位置:网站首页>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;
}
返回回去的变量一定要动态开辟内存,因为局部变量保存在栈上,随着函数的生命周期的结束会内存销毁,而动态开辟的内存是在堆上,函数结束,并不会释放该内存。
边栏推荐
- C#应用程序界面开发基础——窗体控制(3)——文件类控件
- [untitled]
- R language ggplot2 visual faceting, visual facet_wrap bar plot, using strip Text function customize the size of the strip of each facet title in the facet graph (cutimi
- MySQL foundation 06 DDL
- ThinkPHP+Redis实现简单抽奖
- 异步、邮件、定时三大任务
- 力扣 204. 计数质数
- What are the trading forms of spot gold and what are the profitable advantages?
- Leetcode 6103 - minimum fraction to delete an edge from the tree
- 按键精灵打怪学习-多线程后台坐标识别
猜你喜欢

Niu Ke swipes questions and clocks in

Basic concept and implementation of overcoming hash

Telephone network problems

Androd Gradle 对其使用模块依赖的替换

Meituan dynamic thread pool practice ideas, open source

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

Using tensorboard to visualize the model, data and training process
![[androd] module dependency replacement of gradle's usage skills](/img/5f/968db696932f155a8c4a45f67135ac.png)
[androd] module dependency replacement of gradle's usage skills

C#应用程序界面开发基础——窗体控制(1)——Form窗体

JS inheritance and prototype chain
随机推荐
On Fibonacci sequence
MySQL foundation 06 DDL
Detailed explanation of Q-learning examples of reinforcement learning
d,ldc構建共享庫
How is the mask effect achieved in the LPL ban/pick selection stage?
[androd] module dependency replacement of gradle's usage skills
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
MySQL --- 数据库查询 - 条件查询
Look at how clothing enterprises take advantage of the epidemic
[shutter] animation animation (shutter animation type | the core class of shutter animation)
[C language] detailed explanation of pointer and array written test questions
Is there a handling charge for spot gold investment
Top ten regular spot trading platforms 2022
基本远程连接工具Xshell
2022 cable crane driver examination registration and cable crane driver certificate examination
C application interface development foundation - form control (2) - MDI form
Leetcode 6103 - minimum fraction to delete an edge from the tree
The meaning of wildcard, patsubst and notdir in makefile
leetcode 6103 — 从树中删除边的最小分数
SwiftUI 组件大全之使用 SceneKit 和 SwiftUI 构建交互式 3D 饼图(教程含源码)