trap rain water
1 class Solution { 2 public: 3 int trap(int A[], int n) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 if( n==0 )return 0; 7 int volum = 0; 8 stack<int> s; 9 s.push(0); 10 int t; 11 for(int i=1;i<n;i++) 12 { 13 if( A[i] == 0 ) continue; 14 int h = 0; 15 while(!s.empty() ) 16 { 17 t = s.top(); 18 volum += (i-t-1)*abs( min(A[i], A[t]) - h ); 19 if( A[i] >= A[t] ) 20 { 21 s.pop(); 22 // s.push(i); 23 h = A[t]; 24 } 25 else 26 break; 27 } 28 s.push(i); 29 } 30 return volum; 31 32 } 33 };
posted on 2013-07-05 11:52 jumping_grass 阅读(150) 评论(0) 收藏 举报
浙公网安备 33010602011771号