原题链接在这里:https://leetcode.com/problems/3sum-with-multiplicity/

题目:

Given an integer array A, and an integer target, return the number of tuples i, j, k  such that i < j < k and A[i] + A[j] + A[k] == target.

As the answer can be very large, return it modulo 10^9 + 7.

Example 1:

Input: A = [1,1,2,2,3,3,4,4,5,5], target = 8
Output: 20
Explanation:
Enumerating by the values (A[i], A[j], A[k]):
(1, 2, 5) occurs 8 times;
(1, 3, 4) occurs 8 times;
(2, 2, 4) occurs 2 times;
(2, 3, 3) occurs 2 times.

Example 2:

Input: A = [1,1,2,2,2,2], target = 5
Output: 12
Explanation:
A[i] = 1, A[j] = A[k] = 2 occurs 12 times:
We choose one 1 from [1,1] in 2 ways,
and two 2s from [2,2,2,2] in 6 ways.

Note:

  1. 3 <= A.length <= 3000
  2. 0 <= A[i] <= 100
  3. 0 <= target <= 300

题解:

Perform 3Sum and find the target.

Here there are 2 cases:

1. A[l] != A[r], count the frequency of A[l] and A[r], possible number of tuples is leftCount*rightCount.

e.g. 2333, A[l] = 2, A[r] = 3, leftCount = 1, rightCount = 3, number of types is 1*3 = 3.

2. A[l] == A[r], number of tupes is count*(count-1)/2.

e.g. 222, A[l] = 2, A[r] = 2, number of tupes 3*2/2 = 3. There are 3 ways to pick 2 elements from it.

Time Compelxity: O(n^2). n = A.length.

Space: O(1).

AC Java:

 class Solution {
public int threeSumMulti(int[] A, int target) {
if(A == null || A.length == 0){
return 0;
} int res = 0;
int M = 1000000000+7;
Arrays.sort(A);
for(int i = 0; i<A.length-2; i++){
int l = i+1;
int r = A.length-1;
while(l<r){
int sum = A[i] + A[l] + A[r];
if(sum < target){
l++;
}else if(sum > target){
r--;
}else if(A[l] != A[r]){
int leftCount = 1;
int rightCount = 1;
while(l+1<r && A[l]==A[l+1]){
leftCount++;
l++;
} while(l+1<r && A[r]==A[r-1]){
rightCount++;
r--;
} res += leftCount*rightCount;
res = res%M;
l++;
r--;
}else{
// A[l] == A[r]
res += (r-l+1)*(r-l)/2;
res = res%M;
break;
}
}
} return res;
}
}

LeetCode 923. 3Sum With Multiplicity的更多相关文章

  1. [LeetCode] 923. 3Sum With Multiplicity 三数之和的多种情况

    Given an integer array A, and an integer target, return the number of tuples i, j, k  such that i &l ...

  2. 923. 3Sum With Multiplicity - LeetCode

    Question 923. 3Sum With Multiplicity Solution 题目大意: 给一个int数组A和一个目标值target,求满足下面两个条件的组合个数,其中i,j,k分别为数 ...

  3. 【LeetCode】923. 3Sum With Multiplicity 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/3sum-wit ...

  4. 【leetcode】923. 3Sum With Multiplicity

    题目如下: Given an integer array A, and an integer target, return the number of tuples i, j, k  such tha ...

  5. 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)

    转自  http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...

  6. [Leetcode][016] 3Sum Closest (Java)

    题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, ...

  7. LeetCode 15 3Sum [sort] <c++>

    LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...

  8. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  9. [LeetCode] 259. 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

随机推荐

  1. Codeforces Round #604

    Beautiful Regional Contest 题意 题解 代码 Beautiful Sequence 题意 题解 代码 一个思路不够清晰的代码 Beautiful Mirrors with q ...

  2. AtCoder-abc147 (题解)

    A - Blackjack (水题) 题目链接 大致思路: 水题 B - Palindrome-philia (水题) 题目链接 大致思路: 由于整个串是回文串,只要判断前一半和后一半有多少个不同即可 ...

  3. C语言的变参列表 va_list

    1. va_list的基本原理和用法 #include<stdio.h> #include<stdarg.h> void func(int i,char *ch,...){ / ...

  4. C#下IOC/依赖注入框架Grace介绍

    对依赖注入或控制反转不了解的童鞋请先自行学习一下这一设计,这里直接介绍项目和实现步骤. Grace是一个开源.轻巧.易用同时特性丰富.性能优秀的依赖注入容器框架.从这篇IOC容器评测文章找到的Grac ...

  5. (四) Docker 使用Let's Encrypt 部署 HTTPS

    参考并感谢 周花卷 https://www.jianshu.com/p/5afc6bbeb28c 下载letsencrypt镜像(不带tag标签则表示下载latest版本) docker pull q ...

  6. ImportError: cannot import name Namespace

    运行socketServer报错. 解决: pip uninstall python-socketio pip install python-socketio

  7. Fedora 30系统的升级方法

    Fedora 30 已经发布了.你可能希望将系统升级到最新版本的 Fedora.Fedora 工作站版本有图形化升级的方法.另外,Fedora 也提供了一个命令行方法,用于将 Fedora 29 升级 ...

  8. GoogleMap增加标记和路线轨迹的方法

    声明:本文基于JavaScript环境编写. 前言 按照目前的项目需求,我们需要在谷歌地图上标记出当前仓库的位置.司机补货的行车路径.司机当前班次需要补货的机器的位置,同时根据补货状态的不同标记成不同 ...

  9. Java中new和Class.forName的区别

    首先:New = Class.forName("pacage.A").newInstance(); new是关键字,直接创建对象.Class.forName()是一个方法,要求JV ...

  10. 【转载】C#中Convert.ToDouble方法将字符串转换为double类型

    在C#编程过程中,可以使用Convert.ToDouble方法将字符串或者其他可转换为数字的对象变量转换为double类型,Convert.ToDouble方法有多个重载方法,最常使用的一个方法将字符 ...