LeetCode 923. 3Sum With Multiplicity
原题链接在这里: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:
3 <= A.length <= 30000 <= A[i] <= 1000 <= 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的更多相关文章
- [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 ...
- 923. 3Sum With Multiplicity - LeetCode
Question 923. 3Sum With Multiplicity Solution 题目大意: 给一个int数组A和一个目标值target,求满足下面两个条件的组合个数,其中i,j,k分别为数 ...
- 【LeetCode】923. 3Sum With Multiplicity 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/3sum-wit ...
- 【leetcode】923. 3Sum With Multiplicity
题目如下: Given an integer array A, and an integer target, return the number of tuples i, j, k such tha ...
- 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)
转自 http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...
- [Leetcode][016] 3Sum Closest (Java)
题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, ...
- LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- [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 < ...
随机推荐
- pytest_06_fixture之yield实现teardown
上一篇讲到fixture通过scope参数控制setup级别,既然有setup作为用例之前前的操作,用例执行完之后那肯定也有teardown操作. 这里用到fixture的teardown操作并不是独 ...
- 《JAVA高并发编程详解》-程序可能出现死锁的场景
- Python进阶(十)----软件开发规范, time模块, datatime模块,random模块,collection模块(python额外数据类型)
Python进阶(十)----软件开发规范, time模块, datatime模块,random模块,collection模块(python额外数据类型) 一丶软件开发规范 六个目录: #### 对某 ...
- rsync安全
rsync可能泄露敏感文件 常用操作列举整个同步目录或指定目录:Defaultrsync 10.0.0.12:: rsync 10.0.0.12::www/ 下载文件或目录到本地:Defaultrsy ...
- 使用 shell 脚本配置 iOS 工程
APP开发过程中,往往需要在多个网络环境或配置中进行切换,以获取不同配置的APP,甚至有时需要用一套代码经过简单的配置生成不同的APP.而手动配置费时费力,且容易出错.这里介绍用脚本工具,去生成不 ...
- Java文件流下载并提示文件不存在
做文件下载功能的时候,一般使用流的形式下载文件, 如果源文件不存在,下载页面可能就会没有提示,或者一片空白 用户操作之后可能一头雾水,那如何友好提示呢? 想到的有两种 1.可以尝试下载一个名称为:文件 ...
- Windows安装MySQL5.7教程
导读: 我们日常学习可能会需要在本地安装MySQL服务,也遇到过小伙伴探讨关于Windows系统安装MySQL的问题.在这里建议大家安装MySQL5.7版本,当然想尝试8.0版本的同学也可以参考安装. ...
- 原生php phpmailer 发送邮件 email
首先去github下载phpmailer https://github.com/PHPMailer/PHPMailer/ 取得里面的src文件夹中的文件 然后demo如下 首先引用命名空间 use那里 ...
- SHELL脚本编程-expect
SHELL脚本编程-expect 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.expect概述 1>.expect介绍 expect 是由Don Libes基于Tcl( ...
- 用Python添加写入数据到已经存在的Excel的xlsx文件
# coding:utf-8 from openpyxl import load_workbook import openpyxl # 写入已存在的xlsx文件第一种方法 # class Write_ ...