当前位置:网站首页>Leetcode question brushing: hash table 05 (adding four numbers II)
Leetcode question brushing: hash table 05 (adding four numbers II)
2022-06-23 18:25:00 【Taotao can't learn English】
The first 454 topic . Add four numbers II
Given a list of four arrays containing integers A , B , C , D , Calculate how many tuples there are (i, j, k, l) , bring A[i] + B[j] + C[k] + D[l] = 0.
To simplify the problem , be-all A, B, C, D Same length N, And 0 ≤ N ≤ 500 . All integers are in the range of -2^28 To 2^28 - 1 Between , The final result will not exceed 2^31 - 1 .
for example :
Input :
A = [ 1, 2]
B = [-2,-1]
C = [-1, 2]
D = [ 0, 2]
Output :
2
explain :
Two tuples are as follows :
- (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
- (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0
Bring this question , Do you just think about the four-tier cycle like me
/** * @param nums1 * @param nums2 * @param nums3 * @param nums4 * @return * @Version 1.0 */
public int fourSumCount1(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
int count = 0;
for (int i = 0; i < nums1.length; i++) {
for (int j = 0; j < nums1.length; j++) {
for (int k = 0; k < nums1.length; k++) {
for (int l = 0; l < nums1.length; l++) {
if (nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0) {
count++;
}
}
}
}
}
return count;
}

Useless , Don't treat Li Kou like a fool , Do the questions honestly .
Think back to yesterday when you added two numbers , With hashMap, Put... Directly O(N2) Turn into O(N)
It's the same question , The first and second arrays are added separately
hold O(N4) Turn into O(N2)
/** * @param nums1 * @param nums2 * @param nums3 * @param nums4 * @return * @Version 2.0 * * 1,1 0.2 -1.1 * -1 1 2 4 */
public static int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
/** * It can be similar to the sum of two numbers . hold O(N^4) Turn into O(N^2), Think with the sum of two numbers */
// total
int count = 0;
// The length of the array
int n = nums1.length;
// Result set , The key is the desired result , The value is the number of key occurrences , because set Middle key unique
HashMap<Integer, Integer> result = new HashMap<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
//set The middle key does not repeat
if (result.containsKey(-(nums1[i] + nums2[j]))) {
// If the key repeats , To record the number of key occurrences
int value = result.get(-(nums1[i] + nums2[j])) + 1;
result.put(-(nums1[i] + nums2[j]), value);
} else {
result.put(-(nums1[i] + nums2[j]), 1);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (result.containsKey(nums3[i] + nums4[j])) {
// such as 1 2 The sum of appears 4 Time -1,3 4 The sum of the Every occurrence 1 Time 1, Then add 4 Time
count += result.get(nums3[i] + nums4[j]);
}
}
}
return count;
}

Ooh, ooh, ooh !!!
Look at the solution , Just like me , Just use the enhancement for, The speed is much faster
/** * @param nums1 * @param nums2 * @param nums3 * @param nums4 * @return * @Version 3.0 * enhance for Rewrite it * */
public static int fourSumCount3(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
/** * It can be similar to the sum of two numbers . hold O(N^4) Turn into O(N^2), Think with the sum of two numbers */
// total
int count = 0;
// The length of the array
int n = nums1.length;
// Result set , The key is the desired result , The value is the number of key occurrences , because set Middle key unique
HashMap<Integer, Integer> result = new HashMap<>();
for (int i:nums1) {
for (int j :nums2) {
//set The middle key does not repeat
if (result.containsKey(-(i + j))) {
// If the key repeats , To record the number of key occurrences
int value = result.get(-(i + j)) + 1;
result.put(-(i + j), value);
} else {
result.put(-(i + j), 1);
}
}
}
for (int i:nums3) {
for (int j :nums4) {
if (result.containsKey(i + j)) {
// such as 1 2 The sum of appears 4 Time -1,3 4 The sum of the Every occurrence 1 Time 1, Then add 4 Time
count += result.get(i + j);
}
}
}
return count;
}

边栏推荐
- 信用卡产品开发周期从23周缩短至9周,银行运维组织如何转向敏捷?
- 【Wwise】Wwise嵌入Unity后打包出现没有声音问题
- 异步or线程池
- 云原生行业应用崛起,从“可用”到“好用”有多远?
- Paper reading (55):dynamic multi robot task allocation under uncertainty and temporary constraints
- How to make towel washing label
- Implementing Domain Driven Design - using ABP framework - repository
- Establishment and use of SSL VPN (OpenVPN)
- QML类型:Loader
- README
猜你喜欢

论文阅读 (55):Dynamic Multi-Robot Task Allocation under Uncertainty and Temporal Constraints

Deep understanding of padding

云原生行业应用崛起,从“可用”到“好用”有多远?

leetcode刷题:哈希表03 (快乐数)

论文阅读 (47):DTFD-MIL: Double-Tier Feature Distillation Multiple Instance Learning for Histopathology..

【ESP8266-01s】获取天气,城市,北京时间

Landing of global organizational structure control

五星认证!知道创宇通过中国信通院内容审核服务系统评测

反直觉的三门问题,80%的人都会错?

Redis 集群
随机推荐
Latex使用\usepackage{hyperref}报错:paragraph ended before [email protected]@link was complete
【二叉树】翻转二叉树以匹配先序遍历
论文阅读 (55):Dynamic Multi-Robot Task Allocation under Uncertainty and Temporal Constraints
五星认证!知道创宇通过中国信通院内容审核服务系统评测
Redis Cluster
Implementing Domain Driven Design - using ABP framework - repository
Call face recognition exception
2021 excellent TDP members' output data summary is coming, come and watch!!!
Establishment and use of SSL VPN (OpenVPN)
提高效率 Or 增加成本,开发人员应如何理解结对编程?
Stepping mode of research control motor
计算机学院改考后,网络空间安全学院也改考了!南京理工大学计算机考研
csdn涨薪秘籍之Jenkins集成allure测试报告全套教程
Thesis reading (57):2-hydr_ Ensemble: lysine 2-hydroxyisobutyrylation identification with ensemble method (task)
README
深入理解 padding
视频异常检测数据集 (ShanghaiTech)
【華中科技大學】考研初試複試資料分享
聊一聊数据库的行存与列存
Paper reading (56):muti features predction of protein translational modification sites (task)