标签:根据 一点 答案 有用 vector int == 0ms solution
1 #include"000库函数.h" 2 //一点头绪都没有 3 //然后就自己按自己的意思来一遍 4 //好像没有用算法 5 //16ms,让我激动一把 6 7 class Solution { 8 public: 9 int trap(vector<int>& height) { 10 if (height.size() < 2)return 0; 11 int s = 0;//起始点 12 int e = 0;//终止点 13 int v = 0;//接雨水量 14 int t = 0;//计算中间掠过的柱子 15 int i, j; 16 for (i = 0; i < height.size()-1; ++i) { 17 if (height[i] < 1)continue; 18 s = height[i];//找到非0点为起始点 19 for (j = i + 1; j < height.size(); ++j) { 20 if (height[j] >= s) {//找到比起始点大或者相等的点 21 e = height[j]; 22 break; 23 } 24 t += height[j]; 25 if (j == height.size() - 1) {//没有比起始点小的数了 26 --s;//对s进行降高度 27 j = i;//重新遍历 28 t = 0;//重新计算中间掠过的柱子 29 } 30 } 31 v += (s < e ? s : e)*(j - i - 1) - t;//计算体积,记得减去中间的掠过体积 32 t = 0;//下一步重新计算掠过的体积 33 i = j - 1;//换过起始点(即终止点作为新的起始点) 34 } 35 return v; 36 } 37 }; 38 39 //下面是看够力扣题解之后的答案 40 //根据力扣上面的解题提示 41 //尝试着做一下 42 //使用暴力法进行解答 43 //484ms 44 class Solution { 45 public: 46 int trap(vector<int>& height) { 47 int ans = 0; 48 int size = height.size(); 49 for (int i = 1; i < size - 1; i++) { 50 int max_left = 0, max_right = 0; 51 for (int j = i; j >= 0; j--) { //Search the left part for max bar size 52 max_left = max(max_left, height[j]); 53 } 54 for (int j = i; j < size; j++) { //Search the right part for max bar size 55 max_right = max(max_right, height[j]); 56 } 57 ans += min(max_left, max_right) - height[i]; 58 } 59 return ans; 60 } 61 }; 62 // 63 // 64 //使用动态规划20ms 65 // 66 class Solution { 67 public: 68 int trap(vector<int>& height) { 69 if (height.size() == NULL)return 0; 70 int ans = 0; 71 int size = height.size(); 72 vector<int> left_max(size), right_max(size); 73 left_max[0] = height[0]; 74 for (int i = 1; i < size; i++) { 75 left_max[i] = max(height[i], left_max[i - 1]); 76 } 77 right_max[size - 1] = height[size - 1]; 78 for (int i = size - 2; i >= 0; i--) { 79 right_max[i] = max(height[i], right_max[i + 1]); 80 } 81 for (int i = 1; i < size - 1; i++) { 82 ans += min(left_max[i], right_max[i]) - height[i]; 83 } 84 return ans; 85 } 86 }; 87 // 88 // 89 //使用堆栈法28ms 90 // 91 class Solution { 92 public: 93 int trap(vector<int>& height) { 94 int ans = 0, current = 0; 95 stack<int> st; 96 while (current < height.size()) { 97 while (!st.empty() && height[current] > height[st.top()]) { 98 int top = st.top(); 99 st.pop(); 100 if (st.empty()) 101 break; 102 int distance = current - st.top() - 1; 103 int bounded_height = min(height[current], height[st.top()]) - height[top]; 104 ans += distance * bounded_height; 105 } 106 st.push(current++); 107 } 108 return ans; 109 } 110 }; 111 // 112 //使用双指针法16ms 113 // 114 class Solution { 115 public: 116 int trap(vector<int>& height) { 117 int left = 0, right = height.size() - 1; 118 int ans = 0; 119 int left_max = 0, right_max = 0; 120 while (left < right) { 121 if (height[left] < height[right]) { 122 height[left] >= left_max ? (left_max = height[left]) : ans += (left_max - height[left]); 123 ++left; 124 } 125 else { 126 height[right] >= right_max ? (right_max = height[right]) : ans += (right_max - height[right]); 127 --right; 128 } 129 } 130 return ans; 131 } 132 }; 133 134 135 void T042() { 136 Solution s; 137 vector<int>v; 138 v = { 0,1,0,2,1,0,1,3,2,1,2,1 }; 139 cout << s.trap(v) << endl; 140 v = { 4,2,1,2,4 }; 141 cout << s.trap(v) << endl; 142 v = { 0,4,0,3,0,2}; 143 cout << s.trap(v) << endl; 144 145 146 }
标签:根据 一点 答案 有用 vector int == 0ms solution
原文地址:https://www.cnblogs.com/zzw1024/p/10578399.html