18-m-4Sum
来源:互联网 发布:域名商 编辑:程序博客网 时间:2024/05/22 21:59
基本思路跟3sum一样,排序后两边夹,多了一层循环。但多了一层循环,就多了好多判断,尤其是有相同数字时要跳过已成为解的数字组合,同时也要注意要组合到所有数字。提交了不少次,都是要么少解要么重复解,一个个改进后最后AC。不得不再次赞一下leet的测试用例,确实把编写过程中我出现的细微bug都测出来了,当然这也说明自己还需要继续提高。另外这可能是为数不多n³时间度的题目吧,原以为会超时的。
如下:
void SortThreeSum(int num[], int n, int l, int h){ if (l >= h) return; int low = l, high = h; int temp = num[low]; while (low < high) { while (temp < num[high] && low < high) high--; num[low] = num[high]; while (temp >= num[low] && low < high) low++; num[high] = num[low]; } num[low] = temp; SortThreeSum(num, n, l, low - 1); SortThreeSum(num, n, low + 1, h);}int** fourSum(int* nums, int numsSize, int target, int* returnSize) { if (nums == NULL || numsSize < 4) return NULL; int **result = NULL; int size = 1; result = (int **)malloc(sizeof(int *) * size); for (int i = 0; i < size; i++) { result[i] = (int *)malloc(sizeof(int) * 4); memset(result[i], 0, sizeof(int)); } int rIndex = 0; SortThreeSum(nums, numsSize, 0, numsSize - 1); //借用写好的排序 int tempT = 0, low = 0, high = 0; for (int i = 0; i < numsSize - 3; i++) { //[-5,-5,-3,-1,0,2,4,5], -7 //[-5,-3,-1,0,5], 1 //[-3,-3,-2,-2,-2,0,0,4], -1 for (int j = i + 1; j < numsSize - 1; j++) { tempT = target - (nums[i] + nums[j]); low = j + 1, high = numsSize - 1; while (low < high) {// while (nums[low] + nums[high] < tempT && low < high) //递进太快,可能会跳过low中与high的组合// low++;// while (nums[low] + nums[high] > tempT && low < high)// high--; if (nums[low] + nums[high] < tempT) //不用while用if确保循环能比到每一个low+high的组合 low++; else if (nums[low] + nums[high] > tempT) high--; if (low == high) break; if (nums[low] + nums[high] == tempT) { //[-10,-8,-5,2], -21 result[rIndex][0] = nums[i]; result[rIndex][1] = nums[j]; result[rIndex][2] = nums[low]; result[rIndex][3] = nums[high]; rIndex++; *returnSize += 1; result = (int **)realloc(result, sizeof(int *) * (*returnSize + 1)); result[*returnSize] = (int *)malloc(sizeof(int) * 4); memset(result[*returnSize], 0, sizeof(int) * 4); if (nums[low] == nums[low + 1]) { while (nums[low] == nums[low + 1] && low < high) low++; } if (nums[high] == nums[high - 1]) { while (nums[high] == nums[high - 1] && high > low) high--; } low++, high--; } } if (nums[j] == nums[j + 1]) { while (nums[j] == nums[j + 1]) j++; } } if (nums[i] == nums[i + 1]) { while (nums[i] == nums[i + 1]) i++; } } return result;}
0 0
- 18-m-4Sum
- 《LeetBook》leetcode题解(18) : 4Sum[M]
- 【leetcode】454. 4Sum II【M】
- 1-m-Two Sum
- 15-m-3Sum
- 39-m-Combination Sum
- 15. 3Sum M
- leetcode(M) three sum
- LeetCode:M-3 Sum
- 494. Target Sum(M)
- 16-m-3Sum Closest
- 40-m-Combination Sum II
- 15. 3Sum【M】【28】
- leetcode(M) 3sum closest
- (M)Dynamic Programming:Target Sum
- (M)Backtracking:39. Combination Sum
- [Leetcode] #1#15#18 2Sum & 3Sum & 4Sum
- 15. 3Sum && 16 sum closet && 18 4sum
- Pots
- codeforces #309 553C C. Love Triangles(dfs+ 图论)
- swift初体验
- iOS学习笔记 数据库操作(使用FMDB)
- 关于使用 MongoDB Connector for Hadoop 的经验和教训
- 18-m-4Sum
- 【leetcode c++】38 Count and Say
- android adt 最新下载地址23.03
- 回归.Net
- 20150623 javascript实现的简单刷贴
- Fire Game
- 在linux下,解决不能输入也不能退出的问题
- 【软件测试】2、软件测试的概念和基本原则
- JavaSE笔记之<final关键字>