题目要求:

Design an algorithm for the 3-SUM problem that takes time proportional to n2 in the worst case. You may assume that you can sort the n integers in time proportional to n2 or better.

分析:

《算法4》这本书提供的TwoSumFast解法为NlogN,ThreeSumFast解法为N2logN,根据课后练习,要实现3Sum复杂度为N2,建议先把2Sum复杂度实现为N。同时教材提示用排好序的数组可以实现复杂度N。我想了很久,没有发现排好序的数组对复杂度降至N有太大帮助,于是在网上搜索了下大家的做法。网上的大部分都是建议用set或map来做,我决定采用map试试,果然用map很方便。代码如下:

 import java.util.Arrays;
import java.util.HashMap; public class TwoSumLinear {
public static int count(int[] a){
int cnt = 0;
int n = a.length;
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
for(int i =0; i<n;i++){
if(map.get(a[i]) == null) map.put(a[i], i);
Integer negIndex = map.get(-a[i]);
if(negIndex != null && negIndex != i){
System.out.println("a["+negIndex+"]="+(-a[i])+"和a["+i+"]="+a[i]);
cnt++;
}
}
return cnt;
}
public static void main(String[] args){
int[] a = { 30, -40, -20, -10, 40, 0, 10, 5 };
System.out.println(Arrays.toString(a));
System.out.println(count(a));
}
}

3Sum的作业提示可以先将数组排序,基于这个思路,结合写过的2Sum线性实现方法,写出了复杂度为N2的3Sum,个人认为实现的方式已经很精简了。

 import java.util.Arrays;
import java.util.HashMap; public class ThreeSumQuadratic {
public static int count(int[] a, int target) {
Arrays.sort(a);// 数组从小到大排序,后面要使用有序数组的性质简化运算
System.out.println(Arrays.toString(a));
System.out.println("target="+target);
int cnt = 0;
int n = a.length;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < n; i++) {
map.put(a[i], i); //以数组value为key,index为map值
}
for (int i = 0; i < n - 1; i++) {//i不会超过n-2
for (int j = i + 1; j < n; j++) {//j从i+1开始统计,不会超过n-1
int smallValue = a[i] + a[j]; //因为排好序了,所以最开始的a[i]+a[j]
if (smallValue > target) //当a[i]+a[j]>target时没必要计算了,因为后续的查找就会重复
break;
int bigValue = target-smallValue; //计算出对应的数值较大的value
Integer bigIndex = map.get(bigValue); //查找数值较大的value所在的位置
if (bigIndex != null && bigIndex > i && bigIndex > j) {
System.out.println(
"[" + i + "]=" + a[i] + ",[" + j + "]" + a[j] + ",[" + bigIndex + "]" + (bigValue));
cnt++;
}
}
}
return cnt;
} public static void main(String[] args) {
int[] a = { 30, -40, -20, -10, 40, 0, 10, 5 };
System.out.println(count(a,0));
}
}

Coursera Algorithms week1 算法分析 练习测验: 3Sum in quadratic time的更多相关文章

  1. Coursera Algorithms week1 算法分析 练习测验: Egg drop 扔鸡蛋问题

    题目原文: Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg ...

  2. Coursera Algorithms week1 查并集 练习测验:3 Successor with delete

    题目原文: Given a set of n integers S = {0,1,…,N-1}and a sequence of requests of the following form: Rem ...

  3. Coursera Algorithms week1 查并集 练习测验:2 Union-find with specific canonical element

    题目原文: Add a method find() to the union-find data type so that find(i) returns the largest element in ...

  4. Coursera Algorithms week1 查并集 练习测验:1 Social network connectivity

    题目原文描述: Given a social network containing. n members and a log file containing m timestamps at which ...

  5. Coursera Algorithms week3 快速排序 练习测验: Decimal dominants(寻找出现次数大于n/10的元素)

    题目原文: Decimal dominants. Given an array with n keys, design an algorithm to find all values that occ ...

  6. Coursera Algorithms week3 快速排序 练习测验: Selection in two sorted arrays(从两个有序数组中寻找第K大元素)

    题目原文 Selection in two sorted arrays. Given two sorted arrays a[] and b[], of sizes n1 and n2, respec ...

  7. Coursera Algorithms week3 快速排序 练习测验: Nuts and bolts

    题目原文: Nuts and bolts. A disorganized carpenter has a mixed pile of n nuts and n bolts. The goal is t ...

  8. Coursera Algorithms week3 归并排序 练习测验: Shuffling a linked list

    题目原文: Shuffling a linked list. Given a singly-linked list containing n items, rearrange the items un ...

  9. Coursera Algorithms week3 归并排序 练习测验: Counting inversions

    题目原文: An inversion in an array a[] is a pair of entries a[i] and a[j] such that i<j but a[i]>a ...

随机推荐

  1. jsessionid什么时候生成并传递到前端的?

    jsessionid什么时候生成并传递到前端的?     如果客户端请求的cookie中不包含JSESSIONID,服务端调用request.getSession()时就会生成并传递给客户端,此次响应 ...

  2. RTL Compiler之Technology Library

    1 Target Library Design Compiler uses the target library to build a circuit. During mapping, Design ...

  3. 微信开发配置JSSDK,注入权限验证,以及invalid signature签名错误解决

    在微信开发中很多功能都要用到微信JSSDK,关于JSSDK的使用,微信官方的文档已经比较详细,一定要仔细去看文档. <script src="https://res.wx.qq.com ...

  4. Concurrency and Application Design

    Concurrency and Application Design In the early days of computing, the maximum amount of work per un ...

  5. STL源码分析之迭代器

    前言 迭代器是将算法和容器两个独立的泛型进行调和的一个接口. 使我们不需要关系中间的转化是怎么样的就都能直接使用迭代器进行数据访问. 而迭代器最重要的就是对operator *和operator-&g ...

  6. python数字取反~

    >>> a = [1,2,3,4,5,7,6,4,2,10] >>> h = len(a)//2 >>> h 5 >>> ~h ...

  7. 二、Scrapy命令行工具

    本文转载自以下链接:https://scrapy-chs.readthedocs.io/zh_CN/latest/topics/commands.html Scrapy是通过 scrapy 命令行工具 ...

  8. scrapy实例matplotlib脚本下载

    利用scrapy框架实现matplotlib实例脚本批量下载至本地并进行文件夹分类:话不多说上代码: 首先是爬虫代码: import scrapy from scrapy.linkextractors ...

  9. 【CodeCraft-19 and Codeforces Round #537 (Div. 2) C】Creative Snap

    [链接] 我是链接,点我呀:) [题意] 横坐标1..2^n对应着2^n个复仇者的基地,上面有k个复仇者(位置依次给出). 你是灭霸你要用以下方法消灭这k个复仇者: 一开始你获取整个区间[1..2^n ...

  10. 敏捷开发系列学习总结(4)—Git管理工具sourcetree的安装

    现在代码管理都流行用git了,小编以前用过clearcase, svn,vss等.现在用了git后,发现git才是最好的,我觉得它最吸引人的地方应该是它的分布式管理吧.git的具体学习,读者可自己去网 ...