Skip to content

Instantly share code, notes, and snippets.

@jikeytang
Created May 20, 2014 03:49
Show Gist options
  • Save jikeytang/6d8d90c633f9df9e91c2 to your computer and use it in GitHub Desktop.
Save jikeytang/6d8d90c633f9df9e91c2 to your computer and use it in GitHub Desktop.
[ Javascript ] - 20140520-题目3
由杭州-轨道友情提供,也可直接到[csdn答题](http://blog.csdn.net/chriswenwu/article/details/26354361):
求子数组的最大和
一个整型数组,数组里有正数和负数。数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和,求所有子数组的和的最大值。
回复时注意加上下面这句话,才会有语法高亮或格式缩进。
```javascript
@rambo-panda
Copy link

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