Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1545203
  • 博文数量: 327
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 3556
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-05 21:28
个人简介

东黑布衣,流浪幽燕。 真诚善良,值得信赖。

文章分类

全部博文(327)

我的朋友

分类: BSD

2018-03-22 23:36:40






  1. void Bubble_Sort(int* nums,int numsSize) {
  2.     int p,i;
  3.     int temp;
  4.     int flag;
  5.     
  6.     for (p=numsSize-1;p>=0;p--) {
  7.         flag=0;
  8.         for (i=0;i<p;i++) {
  9.             if (nums[i]>nums[i+1]) {
  10.                 temp=nums[i];
  11.                 nums[i]=nums[i+1];
  12.                 nums[i+1]=temp;
  13.                 flag=1;
  14.             }
  15.         }
  16.         if (flag==0) break;
  17.     }
  18. }

  19. int** fourSum(int* nums, int numsSize, int target, int* returnSize) {
  20.     if (!nums || numsSize<4) return 0;
  21.     
  22.     int total=100;
  23.     int** res=(int**)malloc(sizeof(int*)*total);
  24.     for (int n;n<total;n++) {
  25.         res[n]=(int*)malloc(sizeof(int)*4);
  26.     }
  27.     
  28.     Bubble_Sort(nums,numsSize);
  29.     
  30.     int size=0;
  31.     int sum;
  32.     int i,j,m,n;
  33.     
  34.     for (i=0;i<numsSize-3;i++) {
  35.         if (i>0&&nums[i]==nums[i-1]) continue;
  36.         
  37.         for (j=i+1;j<numsSize-2;j++) {
  38.             if (j>i+1&&nums[j]==nums[j-1]) continue;
  39.             
  40.             m=j+1;
  41.             n=numsSize-1;
  42.             
  43.             while (m<n) {
  44.                 sum=nums[i]+nums[j]+nums[m]+nums[n];
  45.                 if (sum==target) {
  46.                     if (size>0&&res[size-1][0]==nums[i]&&res[size-1][1]==nums[j]&&res[size-1][2]==nums[m]&&res[size-1][3]==nums[n]) {
  47.                         m++;
  48.                         n--;
  49.                         continue;
  50.                     }
  51.                     
  52.                     if (size==total) {
  53.                         res=(int**)realloc(res,sizeof(int*)*total);
  54.                         for (int t=size;t<total;t++) {
  55.                             res[t]=(int*)malloc(sizeof(int)*4);
  56.                         }
  57.                     }
  58.                     
  59.                     res[size][0]=nums[i];
  60.                     res[size][1]=nums[j];
  61.                     res[size][2]=nums[m];
  62.                     res[size][3]=nums[n];
  63.                     
  64.                     size++;
  65.                     m++;
  66.                     n--;
  67.                 }
  68.                 else if (sum<target) {
  69.                     m++;
  70.                 }
  71.                 else {
  72.                     n--;
  73.                 }
  74.             }
  75.         }
  76.     }

  77.     *returnSize=size;
  78.     return res;
  79. }


  1. /* Here we implement the quick sort algorithm. */
  2. void swap(int *a, int *b) {
  3.     int temp = *a;
  4.     *a = *b;
  5.     *b = temp;
  6. }
  7. int partition(int *arr, int left, int right) {
  8.     int pi = left;
  9.     for (int i = left; i < right; ++i) {
  10.         if (arr[i] <= arr[right])
  11.             swap(&arr[pi++], &arr[i]);
  12.     }
  13.     swap(&arr[pi], &arr[right]);
  14.     return pi;
  15. }
  16. void quick_sort(int *arr, int left, int right) {
  17.     if (left >= right) return;
  18.     int pi = partition(arr, left, right);
  19.     quick_sort(arr, left, pi-1);
  20.     quick_sort(arr, pi+1, right);
  21. }

  22. /* Here we implement the logic to double size a 2-D array. */
  23. void check_size(int ***rets_ref, int *curr_size, int *max_size) {
  24.     // we need to pass 2-D array as (int ***) to modify in place.
  25.     if (*curr_size < *max_size) return;
  26.     *max_size *= 2; // double size
  27.     int **rets = malloc(sizeof(int *)*(*max_size));
  28.     for (int i = 0; i < *curr_size; ++i) {
  29.         rets[i] = (*rets_ref)[i];
  30.     }
  31.     int **tmp = *rets_ref;
  32.     *rets_ref = rets;
  33.     free(tmp);
  34. }

  35. /* Here we implement the 2-sum, 3-sum and 4-sum logic. */
  36. void twoSum(int *nums, int numsSize, int target, int pi, int ***rets_ref, int *returnSize, int *size, int z1, int z2) {
  37.     int **rets;
  38.     int i = pi, j = numsSize-1, sum, x;
  39.     while (i < j) {
  40.         sum = nums[i] + nums[j];
  41.         if (sum == target) {
  42.             rets = *rets_ref; // may be resized during iteration
  43.             rets[*returnSize] = malloc(sizeof(int)*4);
  44.             rets[*returnSize][0] = z1;
  45.             rets[*returnSize][1] = z2;
  46.             rets[*returnSize][2] = nums[i];
  47.             rets[*returnSize][3] = nums[j];
  48.             (*returnSize)++;
  49.             check_size(rets_ref, returnSize, size); // check and resize
  50.             x = nums[i];
  51.             while(++i < j && x == nums[i]); //avoid duplicate
  52.             x = nums[j];
  53.             while(--j > i && x == nums[j]); //avoid duplicate
  54.         } else if (sum < target) {
  55.             i++;
  56.         } else {
  57.             j--;
  58.         }
  59.     }
  60. }
  61. void threeSum(int *nums, int numsSize, int target, int pi, int ***rets_ref, int *returnSize, int *size, int z1) {
  62.     int **rets;
  63.     for (int i = pi; i < numsSize; ++i) {
  64.         if (i >= numsSize-2)
  65.             break; //no more
  66.         if (i > pi && nums[i] == nums[i-1])
  67.             continue; //avoid duplicate
  68.         if (nums[i]+2*nums[numsSize-1] < target)
  69.             continue; //too small
  70.         if (3*nums[i] > target)
  71.             break; //to large
  72.         if (3*nums[i] == target) {
  73.             rets = *rets_ref;
  74.             if (i+2 < numsSize && nums[i+2] == nums[i]) {
  75.                 rets[*returnSize] = malloc(sizeof(int)*4);
  76.                 rets[*returnSize][0] = z1;
  77.                 for (int j = 0; j < 3; ++j)
  78.                     rets[*returnSize][j+1] = nums[i+j];
  79.                 (*returnSize)++;
  80.                 check_size(rets_ref, returnSize, size); // check and resize
  81.             }
  82.             break;
  83.         }
  84.         twoSum(nums, numsSize, target-nums[i], i+1, rets_ref, returnSize, size, z1, nums[i]);
  85.     }
  86. }
  87. int** fourSum(int *nums, int numsSize, int target, int *returnSize) {
  88.     int **rets;
  89.     int size = numsSize;
  90.     rets = malloc(sizeof(int *)*size);
  91.     *returnSize = 0;
  92.     quick_sort(nums, 0, numsSize-1);
  93.     for (int i = 0; i < numsSize; ++i) {
  94.         if (i >= numsSize-3)
  95.             break; //no more
  96.         if (i && nums[i] == nums[i-1])
  97.             continue; //skip duplicate
  98.         if (nums[i]+3*nums[numsSize-1] < target)
  99.             continue; //too small
  100.         if (4*nums[i] > target)
  101.             break; //to large
  102.         if (4*nums[i] == target) {
  103.             if (i+3 < numsSize && nums[i+3] == nums[i]) {
  104.                 rets[*returnSize] = malloc(sizeof(int)*4);
  105.                 for (int j = 0; j < 4; ++j)
  106.                     rets[*returnSize][j] = nums[i+j];
  107.                 (*returnSize)++;
  108.                 check_size(&rets, returnSize, &size); // check and resize
  109.             }
  110.             break;
  111.         }
  112.         threeSum(nums, numsSize, target-nums[i], i+1, &rets, returnSize, &size, nums[i]);
  113.     }
  114.     return rets;
  115. }



  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. int cmp(const void *a, const void *b)
  4. {
  5. return *((int *)a)
阅读(1289) | 评论(0) | 转发(0) |
0

上一篇:POJ 2454

下一篇:51nod 1140

给主人留下些什么吧!~~