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
.
public class Solution { // dp[i] = max(A[i], dp[i - 1] + A[i]) public int maxSubArray(int[] A) { if (A == null || A.length == 0) return 0; int[] dp = new int[A.length]; dp[0] = A[0]; int max = dp[0]; for (int i = 1; i < A.length; i++) { dp[i] = Math.max(A[i], A[i] + dp[i - 1]); if (dp[i] > max) max = dp[i]; } return max; } }
相关推荐
js js_leetcode题解之53-maximum-subarray.js
c语言入门 C语言_leetcode题解之53-maximum-subarray.c
javascript js_leetcode题解之152-maximum-product-subarray.js
python python_leetcode题解之152_Maximum_Product_Subarray.py
java java_leetcode题解之Maximum Subarray Sum with One Deletion.java
- Maximum Subarray: 寻找一个整数数组中,连续子数组的最大和。 - Spiral Matrix: 给定一个m×n矩阵,以螺旋方式遍历矩阵中的所有元素一次,并且只遍历一次。 - Merge Intervals: 给定一组区间,请合并所有重叠的...
leetcode双人赛 leetcode-solution 闲暇之余,刷一下题,弥补数据结构和算法的短板 ...maximum-subarray 加一 plus-one 合并两个有序数组 merge-sorted-array 杨辉三角 pascals-triangle 杨辉三角 II pa
例如"连续子数组的最大和"(Maximum Subarray)。 6. **动态规划**:数组问题常与动态规划相结合,例如"最长递增子序列"(Longest Increasing Subsequence),通过维护一个动态数组来求解。 7. **二分查找**:在已...
java java_leetcode题解之Maximum Product Subarray.java
leetcode怎么销号 LeetCode-Solutions :green_heart:My own LeetCode solutions ...Subarray Easy 动态规划 0069 Sqrt(x) Easy 二分、牛顿迭代 0070 Climbing Stairs Easy 动态规划 0075 Sort Colors M
easy_Maximum-Subarray 提交链接 / Submit (You need register/login first before submit.) (在提交前你需要先注册或登录) 题目描述 / Description Given an integer array nums, find the contiguous subarray ...
53.Maximum Subarray 70.Climbing Stairs 121.Best Time to Buy and Sell Stock 122.Best Time to Buy and Sell Stock II 123.Best Time to Buy and Sell Stock III 141.Linked List Cycle 142.Linked List Cycle II...
题目如《滑动窗口最大值》(https://leetcode-cn.com/problems/maximum-size-subarray-of-equal-sum/)。 以上就是机试中关于字符串、数组、查找与排序、滑动窗口等高频考点的详细解析。在准备机试时,应重点掌握这些...
Subarray] [70. Climbing Stairs] [101. Symmetric Tree] [104. Maximum Depth of Binary Tree] [121. Best Time to Buy and Sell Stock] [167. Two Sum II - Input array is sorted] Medium [2. Add Two Numbers]
leetcode 编码练习 该存储库基于在添加 leetcode 问题的解决方案时自动化样板代码。 使用单个命令,可以获取问题信息,生成python可执行模板,生成测试文件,并在README末尾更新表。 此存储库用于获取问题信息。 ...
技巧积累异或的性质: 如果a ^ b = c,则a ^ c = b求最大/最小异或值,可以考虑Trie classic problem: leetcode-421.... Maximum Subarray leetcode-992. Subarrays with K Different Integers单调双端加速度-单队列技
Maximum Subarray [53]5. House Robber [198]6. Range Sum Query - Immutable [303]7. Counting Bits [338]8. Palindromic Substrings [647]9. Maximum Length of Pair Chain [646]10. Integer Break [343]11. ...
leetcode 和 oj LeetCode 解决方案 Java 版 Leetcode 问题分类 细绳 8 字符串到整数 ...oj_152_maximum_product_subarray / SRC /溶液/ oj_236_lowest_common_ancestor_of_bt / notes.md SRC /溶液/ oj
leetcode写题闪退 #*的多少代表此题的有意思程度 有几题第一次写的时候思绪比较混乱: *****Regular Expression Matching 2014.10.29 对于Find Minimum in Rotated Sorted Array II 和 Find Minimum in Rotated ...