当前位置:网站首页>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 .
边栏推荐
- 不登陆或者登录解决oracle数据库账号被锁定。
- Create your first Kivy program Hello word (tutorial includes source code)
- 数学知识:能被整除的数—容斥原理
- 按键精灵打怪学习-自动回城路线的判断
- Learn the five skills you need to master in cloud computing application development
- Is there anything in common between spot gold and spot silver
- LDC Build Shared Library
- Arduino DY-SV17F自动语音播报
- Detailed explanation of Q-learning examples of reinforcement learning
- 看疫情之下服装企业如何顺势而为
猜你喜欢

MySQL basics 03 introduction to MySQL types

MySQL foundation 04 MySQL architecture

C application interface development foundation - form control (3) - file control
![[androd] module dependency replacement of gradle's usage skills](/img/5f/968db696932f155a8c4a45f67135ac.png)
[androd] module dependency replacement of gradle's usage skills

dotConnect for PostgreSQL数据提供程序

Machine learning terminology

Niu Ke swipes questions and clocks in

Androd gradle's substitution of its use module dependency

Pytest learning notes (12) -allure feature · @allure Step () and allure attach

Soft exam information system project manager_ Real topic over the years_ Wrong question set in the second half of 2019_ Morning comprehensive knowledge question - Senior Information System Project Man
随机推荐
测试右移:线上质量监控 ELK 实战
英语常用词汇
dotConnect for PostgreSQL数据提供程序
High-Resolution Network (篇一):原理刨析
Detailed explanation of Q-learning examples of reinforcement learning
MySQL
[机缘参悟-36]:鬼谷子-飞箝篇 - 面对捧杀与诱饵的防范之道
【第29天】给定一个整数,请你求出它的因子数
Wireshark data analysis and forensics a.pacapng
[flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)
tail -f 、tail -F、tailf的区别
[shutter] animation animation (the core class of shutter animation | animation | curvedanimation | animationcontroller | tween)
The industrial scope of industrial Internet is large enough. The era of consumer Internet is only a limited existence in the Internet industry
音程的知识的总结
SwiftUI 组件大全之使用 SceneKit 和 SwiftUI 构建交互式 3D 饼图(教程含源码)
What is tone. Diao's story
[system analyst's road] Chapter V double disk software engineering (development model development method)
Now that the teenager has returned, the world's fireworks are the most soothing and ordinary people return to work~
Why can't the start method be called repeatedly? But the run method can?
给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。【剑指Offer】