zoukankan      html  css  js  c++  java
  • lintcode:子数组之和为0

    题目:

    给定一个整数数组,找到和为零的子数组。你的代码应该返回满足要求的子数组的起始位置和结束位置

    样例

    给出[-3, 1, 2, -3, 4],返回[0, 2] 或者 [1, 3].

    解题:

    更新

    子树的和是0,根据给的例子:[-3, 1, 2, -3, 4],其累加和【-3,-2,0,-3,1】中心出现了一个数0 ,同时发现两个-3,第一个-3开始到第二个-3之前的部分就是这个子数组

    数组【1,2,3,-5,3】,其累加和【1,3,6,1,5】,两个1之间的部分就是答案,根据元素数组:不包括第一个1的位置,包括第二个1的位置

    所以:计算累加和,当和的值之前已经出现了,这个区间内的数就是子数组的起始id

    注意:当包括0位置的时候,包括0位置,不包括最后一个位置

    当不包括0位置的时候,包括上一个数位置,不包括当前位置

    这个时候需要加入一个值防止是第一个数,put(0,-1),表示还没有数的时候数是0,起始位置是-1,这样当包括0位置的情况,转化为不包括0的情况

    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> result = new ArrayList<Integer>();
            if(nums == null || nums.length == 0){
                return result;
            }
            HashMap<Integer,int[]> map = new HashMap<Integer,int[]>();
            map.put(0,new int[]{-1});// 第一个元素为 0 id 应该为-1
            int sum = 0;
            for(int i=0;i<nums.length;i++){
                sum += nums[i];
                int[] value = map.get(sum);
                if( value == null){
                    value = new int[]{i};
                    map.put(sum,value);
                }else{
                    result.add(value[0] + 1);
                    result.add(i);
                    return result;
                }
            }
            return result;
        }
    }

    纯暴力,时间超时

    Java程序:

    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> result = new ArrayList<Integer>();
            for(int i=0;i<nums.length;i++){
                if(nums[i]==0){
                    result.add(i);
                    result.add(i);
                    return result;
                }
                for(int j=i+1;j<nums.length;j++){
                    int sum = 0;
                    for(int k = i;k<=j;k++){
                        sum+=nums[k];
                    }
                    if(sum==0){
                        result.add(i);
                        result.add(j);
                        return result;
                    }
                }
            }
            return result;
        }
    }
    View Code

    时间复杂度:O(n3)

    参考编程之美,时间复杂度降为:O(n2),这个转换其实很简单,但是有可能你就不知道

    Java程序:

    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> result = new ArrayList<Integer>();
            for(int i=0;i<nums.length;i++){
                if(nums[i]==0){
                    result.add(i);
                    result.add(i);
                    return result;
                }
                int sum = 0;
                for(int j=i;j<nums.length;j++){
                    sum+=nums[j];
                    if(sum==0){
                        result.add(i);
                        result.add(j);
                        return result;
                    }
                }
            }
            return result;
        }
    }
    View Code

    总耗时: 4127 ms

    转化成Python程序:

    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
        """
        def subarraySum(self, nums):
            # write your code here
            numslen = len(nums)
            for i in range(numslen):
                sum = 0
                for j in range(i,numslen):
                    sum+=nums[j]
                    if sum==0:
                        return [i,j]
    View Code

    总耗时: 974 ms

    时间复杂度,也可以降到O(n)

    这里利用到HashMap,从nums[0] 到nums[nums.length-1]的和,都添加到HashMap中,在这个求和的过程中回出现下面情况:

    sum0->i = sum0->j

    这里就说明sum(i+1)->j的和是0

    这样时间复杂度就是O(n)

    Java程序:

    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
            int numslen = nums.length;
            ArrayList<Integer> result = new ArrayList<Integer>();
            HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
            map.put(0,-1);
            int sum = 0;
            for(int i=0;i<numslen;i++){
                sum+=nums[i];
                if(map.containsKey(sum)){
                    result.add(map.get(sum) + 1);
                    result.add(i);
                    return result;
                }
                map.put(sum,i);
            }
            return result;
        }
    }
    View Code

    总耗时: 4520 ms

    Python程序:

    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
        """
        def subarraySum(self, nums):
            # write your code here
            numslen = len(nums)
            d = {0:-1}
            sum = 0
            for i in range(numslen):
                sum = sum + nums[i]
                if sum in d:
                    return [d.get(sum) + 1,i]
                d[sum] = i 
    View Code

    总耗时: 1361 ms

  • 相关阅读:
    Java基础知识强化之集合框架笔记20:数据结构之 栈 和 队列
    Java基础知识强化之集合框架笔记19:List集合迭代器使用之 并发修改异常的产生原因 以及 解决方案
    模块已加载,但对dllregisterServer的调用失败
    sql回滚
    BAT 批处理脚本 教程
    shell脚本小技巧
    shell if
    REDHAT4.8安装yum
    Linux中文显示乱码解决
    Nginx配置文件详细说明
  • 原文地址:https://www.cnblogs.com/theskulls/p/4872718.html
Copyright © 2011-2022 走看看