当前位置:网站首页>Leetcode skimming questions_ Sum of two numbers II - enter an ordered array
Leetcode skimming questions_ Sum of two numbers II - enter an ordered array
2022-07-03 01:32:00 【Figure throne】
Title Description

java resolvent
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 resolvent
/**
* 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;
}
The variables returned must be dynamically opened up memory , Because local variables are stored on the stack , As the life cycle of the function ends, the memory will be destroyed , The dynamically opened memory is on the heap , End of the function , This memory will not be released .
边栏推荐
- leetcode 6103 — 从树中删除边的最小分数
- MySQL基础用法02
- 按键精灵打怪学习-多线程后台坐标识别
- 2022 Jiangxi Provincial Safety Officer B certificate reexamination examination and Jiangxi Provincial Safety Officer B certificate simulation examination question bank
- Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
- Uniapp component -uni notice bar notice bar
- 海量数据冷热分离方案与实践
- wirehark数据分析与取证A.pacapng
- Learn the five skills you need to master in cloud computing application development
- Appuyez sur l'apprentissage de l'esprit de frappe - reconnaissance des coordonnées de fond multithreadées
猜你喜欢

C application interface development foundation - form control (3) - file control

JDBC courses

简易分析fgui依赖关系工具
![[FPGA tutorial case 6] design and implementation of dual port RAM based on vivado core](/img/fb/c371ffaa9614c6f2fd581ba89eb2ab.png)
[FPGA tutorial case 6] design and implementation of dual port RAM based on vivado core

Androd gradle's substitution of its use module dependency
![[机缘参悟-36]:鬼谷子-飞箝篇 - 面对捧杀与诱饵的防范之道](/img/c6/9aee30cb935b203c7c62b12c822085.jpg)
[机缘参悟-36]:鬼谷子-飞箝篇 - 面对捧杀与诱饵的防范之道

电信客户流失预测挑战赛

Meituan dynamic thread pool practice ideas, open source

Top ten regular spot trading platforms 2022

并发编程的三大核心问题 -《深入理解高并发编程》
随机推荐
C#应用程序界面开发基础——窗体控制(4)——选择类控件
[FPGA tutorial case 6] design and implementation of dual port RAM based on vivado core
按键精灵打怪学习-前台和内网发送后台验证码
2022 Jiangxi Provincial Safety Officer B certificate reexamination examination and Jiangxi Provincial Safety Officer B certificate simulation examination question bank
The meaning of wildcard, patsubst and notdir in makefile
[androd] module dependency replacement of gradle's usage skills
看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-9
Vim 9.0正式发布!新版脚本执行速度最高提升100倍
Now that the teenager has returned, the world's fireworks are the most soothing and ordinary people return to work~
数学知识:Nim游戏—博弈论
Key wizard play strange learning - multithreaded background coordinate recognition
Mathematical Knowledge: Steps - Nim Games - Game Theory
【C语言】指针与数组笔试题详解
并发编程的三大核心问题 -《深入理解高并发编程》
Daily topic: movement of haystack
tp6快速安装使用MongoDB实现增删改查
Pytest learning notes (12) -allure feature · @allure Step () and allure attach
Kivy tutorial how to create drop-down lists in Kivy
What are the trading forms of spot gold and what are the profitable advantages?
Tp6 fast installation uses mongodb to add, delete, modify and check