• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
neverlandly
博客园    首页    新随笔    联系   管理    订阅  订阅

Lintcode: Minimum Subarray

Given an array of integers, find the subarray with smallest sum.

Return the sum of the subarray.

Have you met this question in a real interview? Yes
Example
For [1, -1, -2, 1], return -3

Note
The subarray should contain at least one integer.

 

 1 public class Solution {
 2     /**
 3      * @param nums: a list of integers
 4      * @return: A integer indicate the sum of minimum subarray
 5      */
 6     public int minSubArray(ArrayList<Integer> nums) {
 7         // write your code
 8         int local = nums.get(0);
 9         int global = nums.get(0);
10         for (int i=1; i<nums.size(); i++) {
11             local = Math.min(local+nums.get(i), nums.get(i));
12             global = Math.min(local, global);
13         }
14         return global;
15     }
16 }

 

posted @ 2016-01-09 06:09  neverlandly  阅读(257)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3