题目:在数组中找到一个子数组,让子数组的和是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. BZOJ1569: [JSOI2008]Blue Mary的职员分配(dp 暴力)

    Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 483  Solved: 189[Submit][Status][Discuss] Descriptio ...

  2. oracle中connect by语句的优化

    很多应用中都会有类似组织机构的表,组织机构的表又通常是典型的层次结构(没有循环节点).于是通过组织控制数据权限的时候,许多人都喜欢通过connect by获得组织信息,然后再过滤目标数据. 在有些情况 ...

  3. django+xadmin在线教育平台(十六)

    7-7 modelform 提交我要学习咨询1 对应表userask form会对字段先做验证,然后保存到数据库中. 可以看到我们的forms和我们的model中有很多内容是一样的.我们如何让代码重复 ...

  4. thinkphp 下多图ajax上传图片

    碰到一个项目,有一个比较繁琐的功能6个ajax上传,基本上每个上传逻辑多不一样,记录一下 thinkphp的view页面: id方便找到这个元素 name一定要加 [ ] <div class= ...

  5. Mac系统升级后在终端输入git命令时遇到的问题

    Mac系统升级git会找不到并且报错:xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) ...

  6. C#截取两个字符串间的字符串问题

    string s = "我爱北京天安门和长城"; string s1 = "北京"; string s2 = "和"; int i = s. ...

  7. python中字典的遍历

    用ipython运行情况如下: #新建字典 In [1]: name_cards = {'name':'sunwukong','QQ':'123124','addr':'秦皇岛'} #生成key对象 ...

  8. IDEA Java Web(Spring)项目从创建到打包(war)

    创建Maven管理的Java Web应用 创建新项目,"create new project",左侧类型选择"maven",右侧上方选择自己的SDK,点击&qu ...

  9. How to enable download EXE files from the Sharepoint website

          As we all know,many applications have forbidden to upload and download exe files.Because the e ...

  10. 用命令部署WebPart

    Webpart一般是一个wsp文件,可以在VS里面通过右键来部署.但一般真正的生产服务器上面是不会安装VS的,所以一般情况下是把wsp文件拷贝到服务器上面然后启动PowerShell用命令来部署. 部 ...