当前位置:网站首页>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
- Button wizard play strange learning - go back to the city to buy medicine and add blood
- C#应用程序界面开发基础——窗体控制(4)——选择类控件
- Kivy tutorial how to create drop-down lists in Kivy
- [C language] detailed explanation of pointer and array written test questions
- Niu Ke swipes questions and clocks in
- On Fibonacci sequence
- Work experience of a hard pressed programmer
- What is tone. Diao's story
- 测试右移:线上质量监控 ELK 实战
猜你喜欢
![[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)

Basic remote connection tool xshell

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

12_ Implementation of rolling automatic video playback effect of wechat video number of wechat applet

每日一题之干草堆的移动

MySQL基础用法02

leetcode 2097 — 合法重新排列数对

MySQL

dotConnect for PostgreSQL数据提供程序

MySQL foundation 04 MySQL architecture
随机推荐
【FH-GFSK】FH-GFSK信号分析与盲解调研究
Database SQL language 01 where condition
[FPGA tutorial case 6] design and implementation of dual port RAM based on vivado core
Using tensorboard to visualize the model, data and training process
[fh-gfsk] fh-gfsk signal analysis and blind demodulation research
Mathematical Knowledge: Steps - Nim Games - Game Theory
How is the mask effect achieved in the LPL ban/pick selection stage?
Top ten regular spot trading platforms 2022
On Fibonacci sequence
一位苦逼程序员的找工作经历
Tp6 fast installation uses mongodb to add, delete, modify and check
Detailed explanation of Q-learning examples of reinforcement learning
JS inheritance and prototype chain
按键精灵打怪学习-多线程后台坐标识别
Kivy tutorial - example of using Matplotlib in Kivy app
Arduino DY-SV17F自动语音播报
tp6快速安装使用MongoDB实现增删改查
给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。【剑指Offer】
Mongodb common commands of mongodb series
按键精灵打怪学习-自动回城路线的判断