zoukankan      html  css  js  c++  java
  • [经典] 指定数目与和问题

    Two Sum I

    Given an array of integers, return indices of the two numbers such that they add up to a specific target.

    You may assume that each input would have exactly one solution.

    Example:

    Given nums = [2, 7, 11, 15], target = 9,
    
    Because nums[0] + nums[1] = 2 + 7 = 9,
    return [0, 1].

    分析:如果数组已经排序完,那么只需要双指针从两端往中间夹即可。但是没有排序的话,先排序复杂度就已经O(nlogn)了,所以没法用排序法。事实上是有O(n)的hash法:首先遍历一遍,全部放入hashtable(unordered_set)中,复杂度为O(n);然后再对于每个数x,都在hashtable中找target - x的数在不在其中,如果在,就结束,因为题目说假设只有一个答案,复杂度为O(n);总复杂度为O(n)。

    Two Sum II - Input array is sorted 

    不用排序的排序法,因为数组已经排序完,从两端往中间夹的复杂度与hash法一样也为O(n);但理论时间开销应该比hash法小的。

    Two Sum III - Data structure design

    3 Sum

    与Two Sum题目类似,二元换成了三元。两种解法,复杂度均为O(n^2)

    1. hash法,把两两的和放到一个hashmap(unordered_multimap<int,pair<int,int>>),三个int分别为“和”,“第一个index”,“第二个index”。从头到尾匹配,遇到index在两个之中的,则非解;不在两个中的,则为合法解。
    2. 排序法,然后对任何一个选取x,都在x后面从两边往中间夹,选取和为target - x的

    3 Sum Closest

    这个3 Sum的排序法,可解出,复杂度为O(n ^ 2)。没法用hash法。

    3 Sum Smaller

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 <= i < j < k < n that satisfy the condition nums[i] + nums[j] + nums[k] < target.

    For example, given nums = [-2, 0, 1, 3], and target = 2.

    Return 2. Because there are two triplets which sums are less than 2:

    [-2, 0, 1]
    [-2, 0, 3]
    

    Follow up: Could you solve it in O(n^2) runtime?

    暴力解法是O(n^3)。用Sum的排序法,可接出,复杂度为O(n ^ 2)。

    没法用hash法,顶多用multimap做,但复杂度为O(n^2*logn)。

    4 Sum

    从二元到三元再到四元

    1. 用排序法,复杂度有O(n^3)
    2. 用hash法,复杂度为O(n^2),通过两个unorder_multiple<int, pair<int, int>>,然后比对pair中的index是否重复了,来决定结果是否符合要求。

    【总结】到这里为止,我们基本也可以总结出这NSum类型的解法:

    1. 排序法是万能的,复杂度为O(max(nlogn, n^(N-1));
    2. hash法能也不错,复杂度为O(n^(ceiling(log2N)))
  • 相关阅读:
    开放就像死亡访问之后就能回头——Leo鉴书84
    将博客搬至CSDN
    将博客搬至CSDN
    滚动条
    Perl Pack写的一个数据报表程序
    利用hash 数组打印标题
    Linux显示只显示目录文件
    Linux显示按文件名降序文件
    Linux显示以时间生升序显示文件
    Linux显示按文件大小降序排列
  • 原文地址:https://www.cnblogs.com/littletail/p/5337448.html
Copyright © 2011-2022 走看看