Created
May 20, 2014 03:49
-
-
Save jikeytang/6d8d90c633f9df9e91c2 to your computer and use it in GitHub Desktop.
[ Javascript ] - 20140520-题目3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
由杭州-轨道友情提供,也可直接到[csdn答题](http://blog.csdn.net/chriswenwu/article/details/26354361): | |
求子数组的最大和 | |
一个整型数组,数组里有正数和负数。数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和,求所有子数组的和的最大值。 | |
回复时注意加上下面这句话,才会有语法高亮或格式缩进。 | |
```javascript |
function get(arr){
var c1, c2, ret = [], p = 0, i = 1, j;
arr.push('#');
c1 = arr[0];
for (j = arr.length; i < j; i++) {
c2 = arr[i];
if (c2 == '#' || Math.abs(c2) - Math.abs(c1) != 1) {
if (i - p >= 2) {
ret.push(eval(arr.slice(p, i).join('+')));
}
p = i;
}
c1 = c2;
}
return Math.max.apply(null,ret);
}
console.log(get([1,2,4,5,6,8,2,5,6,7,8,-1,-2,-3]))
var build_arr = (function() {
var length = 200,
arr_more = [],
random;
while (length > -1) {
random = Math.random();
var len = parseInt(random * 50),
arr = [];
length -= len;
while (--len) {
random = Math.random();
arr.push((random > 0.5 ? -1 : 1) * window.parseInt(random * len))
}
arr_more.push(arr);
}
return arr_more;
})(),
length = build_arr.length,
max = 0,
total = 0;
while (--length > -1) {
var arr = build_arr[length];
max = Math.max(Math.max.apply(window, arr), max);
total += new Function('return ' + arr.join('+'))();
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
题目没看太懂