编程习题——Maximum Subarray
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 Test08 {
public static void main(String[] args) {
int[] nums = { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
int[] sums = new int[nums.length];
int max = nums[0];
sums[0] = nums[0];
for (int i = 1; i < nums.length; i++) {
sums[i] = Math.max(nums[i], nums[i] + sums[i - 1]);
max = Math.max(max, sums[i]);
}
System.out.println(max);
}
}
编程习题——Maximum Subarray的更多相关文章
- 3月7日 Maximum Subarray
间隔2天,继续开始写LeetCodeOj. 原题: Maximum Subarray 其实这题很早就看了,也知道怎么做,在<编程珠玑>中有提到,求最大连续子序列,其实只需要O(n)的复杂度 ...
- [LintCode] Maximum Subarray 最大子数组
Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...
- 【leetcode】Maximum Subarray (53)
1. Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...
- 算法:寻找maximum subarray
<算法导论>一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单.寻找maximum subarray稍微复杂点. 题目是这样的:给定序列x = [1, -4, 4, 4, 5, ...
- LEETCODE —— Maximum Subarray [一维DP]
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- 【leetcode】Maximum Subarray
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- maximum subarray problem
In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...
- (转)Maximum subarray problem--Kadane’s Algorithm
转自:http://kartikkukreja.wordpress.com/2013/06/17/kadanes-algorithm/ 本来打算自己写的,后来看到上述链接的博客已经说得很清楚了,就不重 ...
- LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关
Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...
随机推荐
- WinRAR 自动解压 解压完成后,执行批处理文件
部分内容参考网页:http://bbs.kafan.cn/thread-1243208-1-1.html WinRAR 的自动解压文件功能使压缩包也能像 Setup 程序那样,双击后显示一个软件许可, ...
- 实现ModelDriver接口的功能
ModelDriver接口 来自com.opensymphony.xwork2.ModelDriven.是xwork-2.1.2-750.jar包的东西. 下面是源码: package com.ope ...
- Extjs Store 的用法详解
Ext.data.Store的基本用法 在使用之前,首先要创建一个Ext.data.Store的实例,如下面的代码所示. 每个store最少需要两个组件的支持,分别是proxy和reade ...
- org.gradle.process.internal.ExecException:
com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process ...
- 新学习的Python的代码(while循环)
直接上代码: # while.py number = 23 isRun = True while isRun: intt = int(raw_input('请输入一个整数 : ')) if intt ...
- ueditor插件 -- 插入填空题
插入填空题,一个看似小小的需求,但是却是折腾了很9.主要产品那边的要求,空格上面要标有序号,并且再页面当中能够同步空格答案列表. 1.ueditor插件 插件入门,官方的例子还是很简单直接的,对于我们 ...
- TCP 监控工具 TCPMonitor
1, membrane monitor Download: http://www.membrane-soa.org/downloads/archive/monitor-archive.htm 2, a ...
- spoj COT2 - Count on a tree II 树上莫队
题目链接 http://codeforces.com/blog/entry/43230树上莫队从这里学的, 受益匪浅.. #include <iostream> #include < ...
- “_In_opt_z_”: 未声明的标识符
问题 使用VS2010 + WDK 环境进行驱动开发时,编译阶段出现如下等错误提示 “_In_opt_z_”: 未声明的标识符 解决办法 将..\Microsoft Visual Studio 10. ...
- 解密电子书之二:EPD控制芯片
EPD控制芯片大致上相当于计算机的显卡,没了它,所有电子书都变白板.类似显卡中的ATI与NVIDIA,EPD控制芯片中也是两家:Surf(泰信科)和EPSON(爱普生),其中爱普生是最早推出电子纸显示 ...