题解 | #子数组最大连续和#

子数组最大连续和

http://www.nowcoder.com/practice/1718131e719746e9a56fb29c40cc8f95

不保存到数组,时间O(n),空间O(1)。不到双百。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long cur = 0, max = Integer.MIN_VALUE;
        int n = in.nextInt();
        
        for (int i=0; i<n; ++i) {
            int tmp = in.nextInt();
            cur = Math.max(cur + tmp, tmp);
            max = Math.max(cur, max);
        }
        
        System.out.println(max);
    }
}
全部评论

相关推荐

嵌入式求职之路:可以看我经验😂,https://www.nowcoder.com/share/jump/73221730841876945
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务