题目:在数组中找到一个子数组,让子数组的和是k。

思路:先发发牢骚,这两天做题是卡到不行哇,前一个题折腾了三天,这个题上午又被卡住,一气之下,中午睡觉,下午去了趟公司,竟然把namespace和cgroup的架构给搞懂了!所以晚上再来攻克这个问题!上午的做法是这样的,设置一个fast指针,一个slow指针,当【slow,fast】中的值大于k的时候,fast++,反之slow++,这种错误的解法误以为数组是有序的,所以是行不通的,那么这道题的解法是什么呢?然后写了下O(n^3)的解法,果不其然,计算超时!!!没办法,只能查资料啦!

真正被接收的答案真是让人始料未及:

int sum = 0;
    int n = nums.size();
    map<int,int> m;
    int res = 0;

for (int i = 0; i < n; i++) {
        m[sum]++;
        sum += nums[i];
        res += m[sum-k];
    }   
    return res;
map是做啥的啊,以【1,1,1】为例吧,第一轮

m[0]=1,  sum=1          res=m[1-2=-1]=0;

m[1]=1,  sum=1+1=2  res=m[2-2=0]=1;

m[2]=1,  sum=2+1=3  res+=m[3-2=1]=1+1=2;

算法(10)Subarray Sum Equals K的更多相关文章

  1. leetcode 560. Subarray Sum Equals K 、523. Continuous Subarray Sum、 325.Maximum Size Subarray Sum Equals k(lintcode 911)

    整体上3个题都是求subarray,都是同一个思想,通过累加,然后判断和目标k值之间的关系,然后查看之前子数组的累加和. map的存储:560题是存储的当前的累加和与个数 561题是存储的当前累加和的 ...

  2. Subarray Sum & Maximum Size Subarray Sum Equals K

    Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...

  3. Subarray Sum & Maximum Size Subarray Sum Equals K && Subarray Sum Equals K

    Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...

  4. [LeetCode] 325. Maximum Size Subarray Sum Equals k 和等于k的最长子数组

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

  5. Subarray Sum Equals K LT560

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

  6. [leetcode]560. Subarray Sum Equals K 和为K的子数组

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

  7. LeetCode 560. Subarray Sum Equals K (子数组之和等于K)

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

  8. [LeetCode] Subarray Sum Equals K 子数组和为K

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

  9. 560. Subarray Sum Equals K 求和为k的子数组个数

    [抄题]: Given an array of integers and an integer k, you need to find the total number of continuous s ...

随机推荐

  1. 云监控自定义HTTP状态码说明

    您在使用站点监控时,返回的6XX状态码均为云监控自定义HTTP状态码,具体含义如下表所示: 状态码      含义     备注  610  HTTP连接超时      监测点探测您的网站时出现连接超 ...

  2. 关于Ext.js和Ext.Net的杂谈

    最近几年比较火的前端js框架extjs 算是其中的佼佼者.统一的UI设计,强悍的组件及丰富的插件,对浏览器良好的兼容性等优点使得许多公司使用Extjs,同时也使得无数程序猿开始研究这个玩意也包括我在内 ...

  3. php常见面试(Smarty及相关知识)

    1.Smarty简介: 是一种php模板引擎,它分开了后台程序和前台页面内容,使程序员和美工分工合作.还有其它模版引擎实现同样的目的. 2.Smarty使用: 建立php文件: 包含smarty类文件 ...

  4. Hadoop(20)-MapReduce框架原理-OutputFormat

    1.outputFormat接口实现类 2.自定义outputFormat 步骤: 1). 定义一个类继承FileOutputFormat 2). 定义一个类继承RecordWrite,重写write ...

  5. maven 添加自己下载的jar包到本地仓库

    1.在pom文件中添加依赖,其中groupId等变量都自拟. 例如: 2.在命令行执行以下命令,提示build success即表示安装成功. mvn install:install-file -Dg ...

  6. python2.7练习小例子(二十二)

        22):题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和.     程序分析:请抓住分子与分母的变化规律. #!/usr/bin/pyt ...

  7. Android开发——告诉你Adapter应该写在Activity里面还是外面

    0. 前言 本文转载自AItsuki的博客. 首先说明一下为什么要写这么一篇博客:最近看了一些其他人的项目,发现很多项目的做法是建立一个专门存放Adapter类的Package包,也有的项目干脆直接都 ...

  8. 6.JAVA知识点归纳整理

    一.jdk初识与HelloWord: 二.java基础: 2.1 标识符_关键字_数据类型 2.2 数据类型转换 2.3 程序编写格式 2.4 运算符 2.5 分支与for循环 2.6 while_b ...

  9. 常用操作提高效率 之 for 与in

    问题如何而来: 对于刚参加工作的我  批量删除数据通常采用的是前端传递到后台一个对象的id字符串  通过逗号分隔的多个id  或者收的直接是一个id数组 两个原理一样第一个后台要在次使用split(& ...

  10. What to do when Enterprise Manager is not able to connect to the database instance (ORA-28001)

    摘自:http://dbtricks.com/?p=34 If you are trying to connect to the Oracle enterprise Manger and you ge ...