subarray sum

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 List<Integer> subarraySum(int[] nums) {
        // write your code here
        ArrayList<Integer> res = new ArrayList<Integer>();
        for(int i = 0;i < nums.length ; i ++){
            int sum = 0;
            for(int j = i; j < nums.length ;j++){
                sum = sum + nums[j];
                if(sum == 0){
                    res.add(i);
                    res.add(j);
                    return res;
                }
            }
        }
        return res;
    }
}

这个题目两个循环就实现了,但一般两个循环就实现了的题性能都不够好,都应该还有比较好的算法。
这个题和string里那个longest common string有类似之处,都可以dp.稍后再写写这个了。

原文地址:https://www.cnblogs.com/heylinart/p/7651328.html