当前位置:网站首页>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 .
边栏推荐
- tail -f 、tail -F、tailf的区别
- Machine learning terminology
- [interview question] 1369 when can't I use arrow function?
- Learn the five skills you need to master in cloud computing application development
- GDB 在嵌入式中的相关概念
- Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
- leetcode刷题_两数之和 II - 输入有序数组
- [flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)
- 按鍵精靈打怪學習-多線程後臺坐標識別
- 【第29天】给定一个整数,请你求出它的因子数
猜你喜欢
Androd Gradle 对其使用模块依赖的替换
【QT】自定义控件的封装
Wireshark data analysis and forensics a.pacapng
C#应用程序界面开发基础——窗体控制(3)——文件类控件
软考信息系统项目管理师_历年真题_2019下半年错题集_上午综合知识题---软考高级之信息系统项目管理师053
什么是调。调的故事
串口抓包/截断工具的安装及使用详解
C application interface development foundation - form control (2) - MDI form
一比特苦逼程序員的找工作經曆
Meituan dynamic thread pool practice ideas, open source
随机推荐
JDBC courses
2022 coal mine gas drainage examination question bank and coal mine gas drainage examination questions and analysis
Test shift right: Elk practice of online quality monitoring
Now that the teenager has returned, the world's fireworks are the most soothing and ordinary people return to work~
[day 29] given an integer, please find its factor number
C application interface development foundation - form control (2) - MDI form
【面试题】1369- 什么时候不能使用箭头函数?
The difference between tail -f, tail -f and tail
给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。【剑指Offer】
按键精灵打怪学习-自动寻路回打怪点
MySQL foundation 04 MySQL architecture
MySQL --- 数据库查询 - 条件查询
SSL flood attack of DDoS attack
[interview question] 1369 when can't I use arrow function?
How wide does the dual inline for bread board need?
音程的知识的总结
leetcode 2097 — 合法重新排列数对
Key wizard play strange learning - multithreaded background coordinate recognition
MySQL
Key wizard hit strange learning - automatic path finding back to hit strange points