leetcode第53题最大子数和

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/maximum-subarray
给定一个整数数组 nums?,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。

示例:

输入: [-2,1,-3,4,-1,2,1,-5,4],
输出: 6
解释:?连续子数组?[4,-1,2,1] 的和最大,为?6。

思路:找到一个具有最大和的连续子数组肯定需要遍历整个数组 如果出现了负数那么肯定中途会有减小的可能 那么我们必须要用一个变量去存储这个最大值 每次遍历一个元素的时候我们都需要对这个数进行更新

遍历整个数组 当前sum值小于等于0时 那么证明前面的子数组的和对后面的数没有增加的效果 那么就对sum值进行更新 更新为当前数组中的值 否则 更新sum值为前面的子数组的和的值加上当前值 每遍历一个数
都要对整个数组中的最大连续子数组的和的值进行更新 即更新great_sum 一次遍历结束后 那么则找到最大值

public class MaxSubArrayDemo53 {
public static int maxSubArray(int[] nums){
int sum = 0;//用来保存当前的连续数组和的最大值
int great_sum = Integer.MIN_VALUE;//用来保存整个数组的最大连续子数组和的值
for(int val:nums){
if(sum<=0){//求当前连续数组的最大值 如果是小于等于0 那么前面的部分对后面的部分没有增加的作用 故进行舍弃
sum=val;
}else{
sum+=val;
}
//每遍历一个数 那么我们都需要最大值 进行更新
great_sum = Math.max(sum, great_sum);
}
return great_sum;
}
//测试
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.next().toString();
String[] str1 = str.split(",");
int len = str1.length;
int[] nums = new int[len];
for(int i=0; i<len; i++){
nums[i] = Integer.parseInt(str1[i]);
}
int res = maxSubArray(nums);
System.out.println(res);
}
}

原文地址:https://www.cnblogs.com/phantom576/p/11683288.html

时间: 2024-07-30 09:17:44

leetcode第53题最大子数和的相关文章

LeetCode 第53题,最大子序和

题目概述 题目:力扣:53.最大子序和 难易:简单 内容: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4] 输出: 6 解释: 连续子数组 [4,-1,2,1] 的和最大,为 6. 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/maximum-subarray 第一次思路 首先将数组类型分为三类:1.空数组 2.只有一个

LeetCode第[53]题(Java):Maximum Subarray

题目:和最大的子序列 难度:Medium 题目内容: Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. 翻译: 给定一个整数数组nums,找到相邻的子数组(至少包含一个数字),它的总和是最大的,并返回它的和. Example: Input: [-2,1,-3,4,-1,2,1

LeetCode 第 73 题 (Set Matrix Zeroes)

LeetCode 第 73 题 (Set Matrix Zeroes) Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Follow up: Did you use extra space? A straight forward solution using O(mn) space is probably a bad idea. A simple impro

leetcode中第一题twosum问题解答算法的可行性证明

leetcode中第一题twosum问题解答算法的可行性证明 一.引入 关于leetcode中第一题twosum问题,网上已有不少高人做出过解答,并提出了切实可行的算法实现.我在解答该题时参考了博客http://www.zixue7.com/article-9576-1.html的解答.为让读者更直观地阅读和理解本文,先简要摘录以上博客的内容如下: 题目还原 Two Sum Given an array of integers, find two numbers such that they a

Leetcode第五题_Longest Palindromic Substring

Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. Leetcode第5题,题目大概意思是给一个字符串,从中找出最长的回文串,所谓回文串,就是

LeetCode 第三题,Longest Substring Without Repeating Characters

题目: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest s

leetcode第9题-Palindrom Number

这是leetcode的第九题,相对来说比较简单,目的很简单,就是判断一个int型的数是不是回文数.但是有几点需要考虑: 负数应该没有回文数,要加判断!要注意额外的空间申请问题.判断是否是回文数势必要对一个数进行反转,反转的时候就要考虑溢出的问题.实现的代码如下: #include<stdio.h> bool isPalindrom(int x) { if(x<0) return false; else { int tmp=x; int sum=0; while(tmp) { sum=su

LeetCode 第 19 题 (Remove Nth Node From End of List)

LeetCode 第 19 题 (Remove Nth Node From End of List) Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the li

LeetCode 第 342 题(Power of Four)

LeetCode 第 342 题(Power of Four) Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Given num = 16, return true. Given num = 5, return false. Follow up: Could you solve it without loops/recursion? 题目很简单,