1. two sum问题

给定一组序列:[-4 -6 5 1 2 3 -1 7],然后找出其中和为target的一对数

简单做法:两层循环遍历,时间复杂度为n^2

升级版:对给定的序列建立一个hash表,然后只需要外层一层循环就可以了,时间复杂度为n

2. three sum问题

给定一组序列:[-4 -6 5 1 2 3 -1 7],然后找出其中和为target的三个数

简单做法:三层for循环,时间复杂度为n^3

升级版:

  如果看做是two sum升级,可以先从小到大排序,时间复杂度nlogn,然后固定一个数,前面的子列使用two sum的方法,

时间复杂度为n^2;

  也可以先排序,使用两层for循环,遍历子列,获取前两个数,然后对后面的hash过的子列找出另一个,时间复杂度为n^2;

3. four sum问题

可以类比three sum问题,时间复杂度可达到n^3

上述问题我就只能想到这种解法了,但是我感觉对时间复杂度还是不满意

two sum, three sum和four sum问题的更多相关文章

  1. js中sum(2,3,4)和sum(2)(3)(4)都返回9并要求扩展性

    网上有很多关于sum(1)(2)(3),sum(1,2,3)之类的面试题要求输出相同的结果6并要求可以满足扩展,即有多个参数时也能符合题设的要求,所以自己写了部分例子可以大概满足这些面试题的要求 &l ...

  2. 编写一个求和函数sum,使输入sum(2)(3)或输入sum(2,3),输出结果都为5

    昨天的笔试题,做的一塌糊涂,题目考的都很基础而且很细,手写代码对我来说是硬伤啊.其中有一道是这个,然而看到题目的时候,根本没有想到arguments:然后现在就恶补一下. arguments:用在函数 ...

  3. leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III

    112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...

  4. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  5. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  6. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  7. [LeetCode] Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  8. [LeetCode] 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 ...

  9. [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  10. [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

随机推荐

  1. [洛谷3808]【模板】AC自动机(简单版)

    题目大意: 给定$n$个模式串$p(\sum|p_i|\le10^6)$和一个$t(|t|\le10^6)$,求在$t$中被匹配的$p$的个数. 思路: AC自动机模板题,注意$t$中一个字符可能对应 ...

  2. [COCI2015]FUNGHI

    题目大意: 一个环上有8个数,从中选取连续的4个数使得和最大,求最大的和. 思路: 模拟. #include<cstdio> #include<cctype> #include ...

  3. NOI2014 部分题解

    感觉NOI2014的一些题目也是比较好做的... 但是笔者往往有思路却没有想清楚就开始搞了...这样还是不太好.. Day1 T1 起床困难综合征  (我感觉这题应该叫综合征不是综合症...)   a ...

  4. Spark-shell启动脚本解读

    #!/usr/bin/env bash # # Licensed to the Apache Software Foundation (ASF) under one or more # contrib ...

  5. CGCS2000坐标系与其他坐标系间的差异和转换方法

    转自 CGCS2000坐标系与其他坐标系间的差异和转换方法 1954北京坐标系和1980西安坐标系是以天文大地网等经典测量技术为基础的局部坐标系.­ CGCS2000是以地球质量中心为原点的地心大地坐 ...

  6. Java实现中文算数验证码(算数运算+-*/)

    原文:http://blog.csdn.net/typa01_kk/article/details/45050091 /** * creat verification code * */ @Actio ...

  7. eclipse 启动报错 java was started but returned code=13

    eclipse启动不了,出现“Java was started but returned exit code=13......”对话框如下 我的解决方法是:去控制面板--程序--卸载程序和功能下面查看 ...

  8. Java多线程——线程安全问题

    一.什么情况下会产生线程安全问题? 同时满足以下两个条件时: 1,多个线程在操作共享的数据.2,操作共享数据的线程代码有多条. 当一个线程在执行操作共享数据的多条代码过程中,其他线程参与了运算,就会导 ...

  9. 如何Tomcat注册成系统服务

    1.开始->运行(windos+r)中敲cmd,DOS界面2.cd到tomcat的bin目录下3.运行命令service.bat install      这里可以指定注册服务的名字,然后就可以 ...

  10. Angular 学习笔记——run

    <!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> <met ...