当前位置:网站首页>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 .
边栏推荐
- [机缘参悟-36]:鬼谷子-飞箝篇 - 面对捧杀与诱饵的防范之道
- Daily topic: movement of haystack
- Expérience de recherche d'emploi d'un programmeur difficile
- MySQL - database query - condition query
- LDC Build Shared Library
- How is the mask effect achieved in the LPL ban/pick selection stage?
- [FPGA tutorial case 6] design and implementation of dual port RAM based on vivado core
- Type expansion of non ts/js file modules
- The meaning of wildcard, patsubst and notdir in makefile
- d. LDC build shared library
猜你喜欢
Top ten regular spot trading platforms 2022
并发编程的三大核心问题 -《深入理解高并发编程》
音程的知识的总结
Leetcode 6103 - minimum fraction to delete an edge from the tree
Leetcode 2097 - Legal rearrangement of pairs
leetcode 2097 — 合法重新排列数对
Using tensorboard to visualize the model, data and training process
wirehark数据分析与取证A.pacapng
Pytest learning notes (12) -allure feature · @allure Step () and allure attach
MySQL基础用法02
随机推荐
Why is it not recommended to use BeanUtils in production?
[principles of multithreading and high concurrency: 2. Solutions to cache consistency]
强化学习 Q-learning 实例详解
Learn the five skills you need to master in cloud computing application development
Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
Arduino DY-SV17F自动语音播报
【系统分析师之路】第五章 复盘软件工程(开发模型开发方法)
MySQL - database query - condition query
[flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)
串口抓包/截断工具的安装及使用详解
High-Resolution Network (篇一):原理刨析
看疫情之下服装企业如何顺势而为
ThinkPHP+Redis实现简单抽奖
[day 29] given an integer, please find its factor number
【FPGA教程案例6】基于vivado核的双口RAM设计与实现
Database SQL language 01 where condition
英语常用词汇
Niu Ke swipes questions and clocks in
按键精灵打怪学习-自动回城路线的判断
【面试题】1369- 什么时候不能使用箭头函数?