LeetCode 53. Maximum Subarray Java
Subscribe to see which companies asked this question
題目:
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [−2,1,−3,4,−1,2,1,−5,4]
,
the contiguous subarray [4,−1,2,1]
has the largest sum = 6
.
If you have figured out the O(n
題解:
這道題要求 求連續的陣列值,加和最大。
試想一下,如果我們從頭遍歷這個陣列。對於陣列中的其中一個元素,它只有兩個選擇:
1. 要麼加入之前的陣列加和之中(跟別人一組)
2. 要麼自己單立一個數組(自己單開一組)
所以對於這個元素應該如何選擇,就看他能對哪個組的貢獻大。如果跟別人一組,能讓總加和變大,還是跟別人一組好了;如果自己起個頭一組,自己的值比之前加和的值還要大,那麼還是自己單開一組好了。
所以利用一個sum陣列,記錄每一輪sum的最大值,sum[i]表示當前這個元素是跟之前陣列加和一組還是自己單立一組好,然後維護一個全域性最大值即位答案。
程式碼如下:
public class MaximumSubarray { public static void main(String[] args) { // TODO Auto-generated method stub int[] nums = {-2,1,-3,4,-1,2,1,-5,4}; System.out.println(maxSubArray(nums)); } public static int maxSubArray(int[] nums) { int[] sum = new int[nums.length]; int max = nums[0]; sum[0] = nums[0]; for (int i = 1; i < nums.length; i++) { sum[i] = Math.max(nums[i], sum[i - 1] + nums[i]); max = Math.max(max, sum[i]); } return max; } }
相關推薦
LeetCode 53. Maximum Subarray Java
Subscribe to see which companies asked this question 題目: Find the contiguous subarray within an array (containing at least one number) which has the
LeetCode 53. Maximum Subarray
script none div out array emp 我們 pac display https://leetcode.com/problems/maximum-subarray/description/ Find the contiguous subarray wi
[leetcode] 53. Maximum Subarray
nta span ber con 可能 public with tro bar Find the contiguous subarray within an array (containing at least one number) which has the large
[leetcode]53. Maximum Subarray最大子數組和
ont The code exp 最大子數組 math solution XP bar Given an integer array nums, find the contiguous subarray (containing at least one number) wh
leetcode 53-Maximum Subarray(medium)
fir largest upd lee edi ans rec try create Given an integer array nums, find the contiguous subarray (containing at least one number) whi
python實現 leetcode 53. maximum-subarray
按順序求數組裡面連續的數字和的最大值。maximum-subarray 效率最高的一種方式,程式碼很清楚,同樣藉助兩個變數。 class Solution(object): def maxSubArray(self, nums): """ :t
LeetCode(53)-Maximum Subarray
Maximum Subarray Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and
#Leetcode# 53. Maximum Subarray
https://leetcode.com/problems/maximum-subarray/ Given an integer array nums, find the contiguous subarray (containing at least one numbe
#Leetcode# 53. Maximum Subarray
size one Coding leetcode ger ++ ive pub output https://leetcode.com/problems/maximum-subarray/ Given an integer array nums, find the co
[leetcode]53. Maximum Subarray
被44題虐了,先跳個easy緩一緩。easy題就是easy題啊 Solution 1:自己的思路,但是單獨處理了全負數的情況 class Solution { public int maxSubArray(int[] nums) { int i=0;
LeetCode:53. Maximum Subarray(找出陣列中和最大的陣列)
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the l
LeetCode---53. Maximum Subarray
53. Maximum Subarray 題目 思路及解法 這裡我們須要注意子串和子序列之間的差別。 子串是指陣列中連續的若干個元素。而子序列僅僅要求各元素的順序與其在陣列中一致,而沒有連續的要求。對於一個元素數為n的陣列,其含有2n2^{n}2n 個子序列和
leetcode 53. Maximum Subarray(最大子序和)--題解
題目描述 給定一個整數陣列 nums ,找到一個具有最大和的連續子陣列(子陣列最少包含一個元素),返回其最大和。 示例 輸入:[-2, 1, -3, 4, -1, 2, 1, -5, 4] 輸出:6 解釋:連續子陣列 [4, -1, 2, 1] 的和最大,為 6。
leetcode 53. Maximum Subarray 最大子陣列 分治演算法
題目 給定一個整數陣列,找出和最大的連續子陣列(至少包含一個元素)並返回最大和。 嘗試使用分治演算法實現 思路 參考了《演算法導論》裡關於分治的講解,正好就是這個題目,不過它的方法比較細緻,把最大子陣列的位置也求出來了。這道題只要求最大和,我簡化了一下。思路是
[leetCode]53. Maximum Subarray&&動態規劃
Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [
leetcode 53 Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. E
leetcode 53 Maximum Subarray (求最大子陣列的和)
題目要求 (高頻題) 給定一個整數陣列nums,找到具有最大和的連續子陣列(包含至少一個數字)並返回其和。 示例 Input: [-2,1,-3,4,-1,2,1,-5,4], Output: 6 Explanation: [4,-1,2,1] has the large
[Leetcode] 53. Maximum Subarray(最大子串和問題)
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. Example
[python] LeetCode 53. Maximum Subarray
題目描述 原題連結地址:https://leetcode.com/problems/maximum-subarray/description/ Given an integer array nums, find the contiguous subarray (containing at
LeetCode [53. Maximum Subarray]
Question Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example,