当前位置:   article > 正文

力扣11题盛最多水的容器 c语言解题思路_c语言最大容器

c语言最大容器

题目

        11.给定一个长度为 n 的整数数组 height 。有 n 条垂线,第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。返回容器可以储存的最大水量。

        说明:你不能倾斜容器。

        其实就是判断两边相距距离和两边最小值的乘积,结果只需要用一个max比较就行了,不需要输出两边和底部长度就很简单。

1.第一种解法:双指针

        如果是c语言学完,在判断这题的第一反应大部分应该都会是双指针,指针虽然比较难理解,但是不管在程序还是学习中,一般难理解的都是为了简便程序运行而做出的高级运用。

        当然,我的这段代码很长,看不懂不要紧,因为有更简便的方法。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. //主函数
  6. int main(int argc, char const *argv[])
  7. {
  8. //水的两边长度数据
  9. int nums[] = {1,8,6,2,5,4,8,3,7};
  10. //需要几个数组
  11. int size = sizeof(nums)/sizeof(nums[0]);
  12. //
  13. //用来计算每个数组的元素个数
  14. int col = 0;
  15. //
  16. //用来记录当前的面积数据
  17. int childnums[size * sizeof(int)];
  18. memset(childnums,0,size * sizeof(int));
  19. //
  20. //建立数组记录元素个数col
  21. int colsize[size*sizeof(int)];
  22. memset(colsize,0,size * sizeof(int));
  23. //
  24. //建立一个堆空间用来记录每个数组的首地址,用二级指针allstr访问堆空间
  25. int **allstr = (int **)calloc(size,sizeof(int *));
  26. if(allstr == NULL)
  27. {
  28. perror("calloc failed:");
  29. return -1;
  30. }
  31. //
  32. //计算
  33. for(int i = 0; i < size -1;i++)
  34. {
  35. col = 0;
  36. for(int j = i+1; j < size; j++)
  37. {
  38. //对比两边的长度,计算盛水量,(盛水量是短边乘底),并且记录到数组当中
  39. childnums[col++] = (nums[i] > nums[j] ? nums[j]*(j-i) : nums[i]*(j-i));
  40. }
  41. //
  42. //申请堆空间存放数组,并且由指针temp访问堆空间
  43. int* temp = (int *)calloc(col,sizeof(int));
  44. memcpy(temp,childnums,col*sizeof(int));
  45. //
  46. //将temp的首元素地址赋给allstr[i]
  47. allstr[i] = temp;
  48. //
  49. //记录每个子集的元素个数
  50. colsize[i] = col;//数组的元素是 8 7 6 5 4 3 2 1
  51. }
  52. //
  53. //判断前面二级指针是否使用错误
  54. // for(int i = 0; i <size; i++)
  55. // {
  56. // for(int j = 0;j < colsize[i];j++)
  57. // {
  58. // printf("%d ",allstr[i][j]);
  59. // }
  60. // printf("\n");
  61. // }
  62. //
  63. // //建立一个数组用来存储
  64. int nums1[size*sizeof(int)];
  65. memset(nums1,0,size * sizeof(int));
  66. for(int i = 0; i < size; i++)
  67. {
  68. int k = 0;
  69. for(int j = 0;j < colsize[i] ; j++)
  70. {
  71. k = (allstr[i][j] > k ? allstr[i][j] : k);
  72. }
  73. nums1[i] = k;
  74. }
  75. // //
  76. // //判断nums1是否可以正常输入
  77. // for(int i = 0; i < size; i++)
  78. // {
  79. // printf("%d ",nums1[i]);
  80. // }
  81. // //
  82. int max = 0;
  83. for(int i = 0; i < size; i++)
  84. {
  85. max = (max > nums1[i] ? max :nums1[i]);
  86. }
  87. printf("可以容纳的水量最大值是:%d\n", max);
  88. //
  89. free(allstr);//释放堆空间
  90. allstr = NULL;
  91. //
  92. return 0;
  93. }

2.第二种解法:数组

        如果能看懂第一种解法的话(没看也不要紧),会发现指针在这里面的作用多余了,因为我们只需要计算最大值就可以了,指针的许多数据保存我们都用不到,这个时候就可以更简便一些,使用数组只将每个边与剩下的边乘积的最大值保存下来,不仅节省了空间还缩短了代码,增加了代码的可读性。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. // //主函数
  6. int main(int argc, char const *argv[])
  7. {
  8. //水的两边长度数据
  9. int nums[] = {1,8,6,2,5,4,8,3,7};
  10. //需要几个数组
  11. int size = sizeof(nums)/sizeof(nums[0]);
  12. //
  13. //用来计算当前数组的元素个数
  14. int col = 0;
  15. //
  16. //用来记录当前的面积数据
  17. int childnums[size * sizeof(int)];
  18. memset(childnums,0,size * sizeof(int));
  19. //
  20. //建立一个数组用来存储
  21. int nums1[size*sizeof(int)];
  22. memset(nums1,0,size * sizeof(int));
  23. //
  24. //计算
  25. for(int i = 0; i < size -1;i++)
  26. {
  27. col = 0;
  28. for(int j = i+1; j < size; j++)
  29. {
  30. //对比两边的长度,计算盛水量,(盛水量是短边乘底),并且记录到数组当中
  31. col = (nums[i] > nums[j] ? nums[j]*(j-i) : nums[i]*(j-i));
  32. }
  33. //
  34. for(int i = 0; i < size; i++)
  35. {
  36. int k = 0;
  37. for(int j = 0;j < col ; j++)
  38. {
  39. k = (childnums[i] > k ? childnums[i] : k);
  40. }
  41. nums1[i] = k;
  42. }
  43. }
  44. //
  45. int max = 0;
  46. for(int i = 0; i < size; i++)
  47. {
  48. max = (max > nums1[i] ? max :nums1[i]);
  49. }
  50. printf("可以容纳的水量最大值是:%d\n", max);
  51. //
  52. return 0;
  53. }

3.第三种解法:for循环

        有了简化的意识后,我就在思考,既然指针可以简化成数组,那数组能不能更加简化呢?当然能,因为我们只需要输出最大值,理论上一个只需要保存一个数字就可以了,这个时候我们就可以把数组也去掉,只留下一个max用来保存和输出最大值就可以了。

        我们可以直接将max放到计算容积的计算中去,判断max和容积哪个大,将大的值赋给max,然后进行下一个容积的判断,最终留下来的max就是最大的容积了。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. // //主函数
  6. int main(int argc, char const *argv[])
  7. {
  8. //水的两边长度数据
  9. int nums[] = {1,8,6,2,5,4,8,3,7};
  10. int size = sizeof(nums)/sizeof(nums[0]);
  11. int col = 0;
  12. int max = 0;
  13. //计算
  14. for(int i = 0; i < size -1;i++)
  15. {
  16. col = 0;
  17. for(int j = i+1; j < size; j++)
  18. {
  19. col = (nums[i] > nums[j] ? nums[j]*(j-i) : nums[i]*(j-i));
  20. max = max > col ? max : col;
  21. }
  22. }
  23. printf("可以容纳的水量最大值是:%d\n", max);
  24. //
  25. return 0;
  26. }

4.解法四:for循环-2

        如果把我的第三种方法直接用到力扣中去,会发现可以运行,但是提交会显示:运行时间超时,这个时候我们就要提到时间复杂度了。

        假设我的数组长度是s,我的第一个数字需要和后面的数字一共计算(s-1)的次数,第二个数字需要计算(s-2)的次数,以此类推,就相当于1=2+...+s的次数,相当于(s(s+1))/2的次数,非常麻烦。

        这个时候我们可以从两边同时开始计算,判断两边长度,在完成一次乘积后判断两边哪个长度短(数字更小),就将其延伸下一位。

        例如num={1,2,3,4,5,9,8,7,6},先计算乘积,然后判断1和6的大小,1的下标加一到2位,然后用2和6判断容积,,最后计算max,这样会节省很多不必要的计算,比如1和2的乘积等。只需压迫计算s-1次就可以了,大大简便了时间复杂度。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. // //主函数
  6. int main(int argc, char const *argv[])
  7. {
  8. //水的两边长度数据
  9. int nums[] = {1,8,6,2,5,4,8,3,7};
  10. int size = sizeof(nums)/sizeof(nums[0]);
  11. int L = 0;//左边数组下标
  12. int R = size -1;//右边数组下标
  13. int max = 0;
  14. while(L < R)
  15. {
  16. int hight = nums[L] > nums[R] ? nums[R] : nums[L];//判断两边哪个数字小,将小值赋给hight
  17. int wide = (R - L);//判断两下标相距长度
  18. int are = wide * hight;//计算乘积
  19. if(max < are)//如果max小就将are给max,否则max就不改变
  20. {
  21. max = are;
  22. }
  23. if(nums[L] < nums[R])
  24. {
  25. L++;
  26. }
  27. else
  28. {
  29. R--;
  30. }
  31. }
  32. printf("可以容纳水的最大容积为:%d\n", max);
  33. return 0;
  34. }

力扣解法标准形式

        如果是第一次使用力扣解题会发现自己写的代码在力扣上无法运行,因为力扣相当于为你准备好了输入输入和标准的数据,只需要写出计算的函数就可以了。

  1. int maxArea(int* height, int heightSize){
  2. }

        这段就是力扣给我们准备的子函数函数名(可以不用管)以及我们需要使用的数组名height,数组内容力扣已经自己准备好了,还有数组长度heightSize。

        我们只需要将自己的函数写出来,然后返回max的值就可以了。

  1. int indexLeft = 0;
  2. int indexRigt = heightSize -1;
  3. int maxAre = 0;
  4. while(indexLeft < indexRigt)
  5. {
  6. int hight = height[indexLeft] > height[indexRigt] ? height[indexRigt] : height[indexLeft];
  7. int wide = (indexRigt - indexLeft) ;
  8. int are = wide * hight;
  9. if(maxAre < are)
  10. {
  11. maxAre = are;
  12. }
  13. if(height[indexLeft] < height[indexRigt])
  14. {
  15. indexLeft++;
  16. }
  17. else
  18. {
  19. indexRigt--;
  20. }
  21. }
  22. return maxAre;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/146144
推荐阅读
相关标签
  

闽ICP备14008679号