2017-04-18 21 views
0

をleetcodeするデバッグJavaソリューションのに苦労:https://leetcode.com/problems/trapping-rain-water/#/description私はここに、この問題を解決しようとしていたアルゴリズム

そして、私のコードが正しくない答えを提供しているが、私は理由を理解することはできません。私はそれを見て私の頭の中を走ると、何が間違っているのか分かりません。

私の解決策は次のとおりです。 (できるだけ効率的な方法論に関する情報を私に提供しないでください。私自身でそれを試してみたいと思います)。

public class Solution { 
    public int trap(int[] height) { 
     int totalWaterTrapped = 0; 
     for (int i = 0; i < height.length; i++){ 
      if (i == 0){ 
       continue; 
      } else if (i == (height.length - 1)){ 
       continue; 
      } else { 
       int tallestLeftwardHeight = height[i]; 
       for (int x = i; x >= 0; x--){ 
        if (height[x] > tallestLeftwardHeight){ 
         tallestLeftwardHeight = x; 
        } 
       } 
       int tallestRightwardHeight = height[i]; 
       for (int y = i; y < height.length; y++){ 
        if (height[y] > tallestRightwardHeight){ 
         tallestRightwardHeight = y; 
        } 
       } 

       totalWaterTrapped += (Math.min(tallestLeftwardHeight, tallestRightwardHeight) - height[i]); 
      } 
     } 
     return totalWaterTrapped; 
    } 
} 

ご協力いただきありがとうございます。

+1

が、ここで問題文を追加し、それ –

答えて

0
   if (height[x] > tallestLeftwardHeight){ 
        tallestLeftwardHeight = x; 
       } 

   if (height[y] > tallestRightwardHeight){ 
        tallestRightwardHeight = height[y]; 
       } 
+0

AGH、そのような愚かなエラーにないリンクでなければなりません

 if (height[x] > tallestLeftwardHeight){ tallestLeftwardHeight = height[x]; } 

 if (height[y] > tallestRightwardHeight){ tallestRightwardHeight = y; } 

する必要があります!本当にありがとう、本当に! –

関連する問題