首页 > 其他 > 详细

[lintcode easy]subarray sum

时间:2015-12-02 07:54:02      阅读:260      评论:0      收藏:0      [点我收藏+]

Subarray Sum

Example

Given [-3, 1, 2, -3, 4], return [0, 2] or [1, 3].

Note

There is at least one subarray that it‘s sum equals to zero.

 

public class Solution {
    /**
     * @param nums: A list of integers
     * @return: A list of integers includes the index of the first number 
     *          and the index of the last number
     */
    public ArrayList<Integer> subarraySum(int[] nums) {
        // write your code here
        ArrayList<Integer> list=new ArrayList<Integer>(0);
        if(nums.length==0) return list;
        int n=nums.length;

        for(int i=0;i<n;i++)
        {
            int sum=0;
             for(int j=i;j<n;j++)
            {
                sum += nums[j];
                
                if(sum==0)
                {
                    list.add(i);
                    list.add(j);
                    return list;
                }
                
            }
        }
        
        
        return list;
    }
}

 

[lintcode easy]subarray sum

原文:http://www.cnblogs.com/kittyamin/p/5011880.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!