当前位置:网站首页>Array target two integers and return their array subscripts
Array target two integers and return their array subscripts
2022-06-10 10:33:00 【InfoQ】
1、 Background
- You can assume that each input corresponds to only one answer . however , The same element in the array cannot be repeated in the answer .
- You can return the answers in any order .
2、 Code implementation
public class Solution {
public static void main(String[] args) {
int[] nums = new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4};
// System.out.println(containsDuplicate(nums));
System.out.println(maxSubArray(nums));
}
/**
* Give you an array of integers nums, Please find a continuous subarray with the largest sum ( A subarray contains at least one element ), Return to its maximum and , A subarray is a continuous part of an array
*
* @param nums
* @return
*/
public static int maxSubArray(int[] nums) {
if (nums.length == 1) {
return nums[0];
}
int sum = Integer.MIN_VALUE;
int count = 0;
for (int i = 0; i < nums.length; i++) {
count += nums[i];
// Take the maximum value of interval accumulation ( It is equivalent to continuously determining the termination position of the maximum subsequence )
sum = Math.max(sum, count);
if (count <= 0) {
// It is equivalent to resetting the starting position of the maximum sub order , Because when you encounter a negative number, you must pull down the sum
count = 0;
}
}
return sum;
}
}
3、 Result display
6
Process finished with exit code 0
边栏推荐
- 【730. 统计不同回文子序列】
- 12892. sub integer (OJ, getting started)
- xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Deve
- 62. different paths - Dynamic Planning
- [golang] input and condition control through BMI index learning console
- 随机生成数字字母(大写)组合
- PV操作每日一题-缓冲区问题(进阶版)
- Composite mode example
- PV操作每日一题-橘子苹果问题(初阶版)
- C 12 循环队列Circular queue
猜你喜欢

"Isolation insurance" has become a "net red" product, but many policyholders say that it is difficult to settle claims
![[interesting reading] deepinf: social influence prediction with deep learning](/img/67/2a90dea2f129245c4a60a5e079340f.png)
[interesting reading] deepinf: social influence prediction with deep learning

【兴趣阅读】DeepInf: Social Influence Prediction with Deep Learning

效率工具 : uTools

Mixer: an indispensable component of video conference recording

Today, 19:30 | graphics special session - Gao Lin, teacher team of Institute of computing technology, Chinese Academy of Sciences

2023 Wangdao C language training camp (binary search tree - sequential search - half search)

干货!基于掩码标签平滑的机器翻译模型训练方法

Xcode8.3.2 automatic packaging script

It is very convenient to make a data analysis crosstab with one line of code
随机推荐
【并发编程JUC】创建线程的四种方式
axure添加下拉菜单联动
Question bank and answers of 2022 metal and nonmetal mine hoist operation examination
"Great freehand flower and bird painting master Mr. likuchan" blockbuster digital collection launched in the whole network
HPCA名人堂成员蒋晓维博士,任职大禹智芯首席科学家
仿淘宝商品详情,下拉切换到图片详情,上拉切换到图文简介
六、观察者模式
Neo hacker song's award-winning list has been announced. Who owns tens of thousands of dollars of gold?
leetcode18子集问题
62. 不同路径-动态规划
Fastadmin uses phpexcel to export tabular data to excel
【高并发】关于乐观锁和悲观锁,蚂蚁金服面试官问了我这几个问题!!
Some problems in using message queue service in thinkphp6
Print: Entry, ':CFBundleIdentifier', Does Not Exist
Drawing curve with MATLAB
ADB 日志抓取
Tami dog shares | real estate enterprise mergers and acquisitions are only increasing, so where to find these projects?
数组目标值target两个整数,并返回它们的数组下标
Industrial Internet Architecture
selenium分布式测试