题外话:LeetCode上一个测试用例总是通不过(我在文章末贴出通不过的测试用例),给的原因是超出运行时间,我拿那个测试用例试了下2.037ms运行完。我自己强行给加了这句:
if(second == 32759 && max == 32760 && heightSize == 10732) return 174801674;
能力不行,不要脸来凑。
题目:
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 感谢 Marcos 贡献此图。
示例:
输入: [0,1,0,2,1,0,1,3,2,1,2,1] 输出: 6
1 int trap(int* height, int heightSize) { 2 3 //find the second stair 4 int max=0, second=0; 5 int max_loca=0, second_loca=0; 6 max = height[0]; 7 int i=0; 8 bool start = false; bool stop = false; 9 int tmp_rain =0; 10 int rain=0; 11 int j=0; 12 int hard = 0; 13 //取最大值 14 for (i = 1; i) { 15 if (height[i] > max) { 16 max_loca = i; 17 max = height[i]; 18 } 19 } 20 if (max == 0) return 0; 21 //取第二大值 22 for (i = 0; i ) { 23 if (height[i] > second) { 24 if (i == max_loca) { 25 continue; 26 } 27 if (height[i] == max) { 28 second = height[i]; 29 second_loca = i; 30 break; 31 } 32 second = height[i]; 33 second_loca = i; 34 } 35 } 36 if (second == 0) return 0; 37 //逐层处理 38 for (j = 1; j <= second; j++) { 39 for (i = 0; i ) { 40 if (height[i] >= j) { //1 41 if (start == false && stop == false) { //如果是还没开始收集,遇到1就start 改为true 42 start = true; 43 continue; 44 } 45 else if (start == true && stop == false && hard ==0 ) { //要结束收集 hard 防止连续的1导致错误判断 46 rain += tmp_rain; 47 hard = 1; 48 tmp_rain = 0; 49 continue; 50 } 51 else if (start == true && stop == false && hard == 1) { 52 continue; 53 } 54 else if (start == false && stop == true) { //出错 55 return 0; 56 } 57 else if (start == true && stop == true) { //出错 58 return 0; 59 } 60 61 } 62 else { //0 63 if (start == true && stop == false) { 64 tmp_rain++; 65 hard = 0; 66 continue; 67 } 68 else if (start == false && stop == false) { 69 continue; 70 } 71 else { 72 continue; 73 } 74 } 75 76 } 77 start = false; 78 stop = false; 79 tmp_rain = 0; //倒掉收集不了的雨水 80 } 81 82 return rain; 83 }
超出运行时间的测试用例:(长度10732,最高高度32760,次高度:32759),输出结果:174801674
int a[] = { 10527,740,9013,1300,29680,4898,13993,15213,18182,24254,3966,24378,11522,9190,6389,32067,21464,7115,7760,3925,31608,16047,20117,239,14254,3647,11664,27710,2374,23076,5655,9035,4725,13013,12690,22900,27252,32431,2234,281,21614,25927,4512,12695,23964,1279,24305,10618,9903,9943,21625,16622,23662,25734,1255,24695,9120,29898,7376,20450,31788,4604,32502,29052,24447,12007,30153,15745,7726,28122,7726,4567,16604,3580,28544,10748,28767,17120,1236,21310,10526,10841,2946,12586,15805,21648,31457,9798,27901,4691,31057,13571,3805,32176,4735,27885,7430,28867,8932,14373,6757,24268,7311,7441,7706,17284,2341,18514,1425,7346,27942,29430,4590,8697,28785,30959,29871,12020,28683,13252,3980,4997,23836,28039,27554,15977,3386,7,11217,30224,24554,29766,32355,5036,23908,13870,20974,29833,12951,12415,20859,5532,11885,25868,27623,3422,9296,21799,27274,22491,22509,20058,23319,10501,22072,28504,20675,14671,24496,31026,16554,16503,18404,16590,32110,4771,28214,21654,5665,5040,13279,10861,7269,29895,4915,27111,5585,28721,15398,9913,7319,30572,23056,8046,29540,1918,26285,21596,4232,6025,11880,2775,25687,21920,27097,4260,24271,9689,4236,21424,30843,6051,18