zoukankan      html  css  js  c++  java
  • 查找表_leetcode18

    # 解题思路:查找表 20190302 找工作期间
    # 哈希表能将查找的复杂度从o(n)降到 o(1)
    class Solution(object):
    def fourSum(self, nums, target):
    """
    :type nums: List[int]
    :type target: int
    :rtype: List[List[int]]
    """

    ln = len(nums)
    res = set()
    dict = {}
    if ln < 4: return []
    nums.sort()
    for p in range(ln):
    for q in range(p + 1, ln):
    if nums[p] + nums[q] not in dict:
    dict[nums[p] + nums[q]] = [(p, q)]
    else:
    dict[nums[p] + nums[q]].append((p, q))

    for i in range(ln):
    for j in xrange(i + 1, ln - 2):
    T = target - nums[i] - nums[j]
    if T in dict:
    for k in dict[T]:
    if k[0] > j:
    res.add((nums[i], nums[j], nums[k[0]], nums[k[1]]))

    return [list(i) for i in res]
  • 相关阅读:
    根据坐标点画图形
    js 解析geojson格式
    devexpress 安装
    DataTable 获取列名
    ADO.NET 注册
    css:outline
    javascript函数sort
    引用类型-2015/10/06
    2015-11-02-js
    jquery
  • 原文地址:https://www.cnblogs.com/lux-ace/p/10546903.html
Copyright © 2011-2022 走看看