c# leetcode11 (暴力破解+双指针) 盛最多水的容器

方法一:暴力破解法

		public static int MaxArea(int[] height)
		{
			int maxArea = 0, temporary = 0;
			for (int i = 0; i < height.Length; i++)

				for (int j = i + 1; j < height.Length; j++)
				{
					//temporary = (j - i) * height[j] > (j - i) * height[i] ? (j - i) * height[i] : (j - i) * height[j]; 
					temporary = Math.Min((j - i) * height[j], (j - i) * height[i]);
					maxArea = Math.Max(temporary, maxArea);
				}
			return maxArea;
		}

Math 比 三项表达式 速度要快一点

temporary = Math.Min((j - i) * height[j], (j - i) * height[i]);

temporary = (j - i) * height[j] > (j - i) * height[i] ? (j - i) * height[i] : (j - i) * height[j]; 

调用:

		public static void Main(string[] args)
		{
			int[] arry = { 1, 8, 6, 2, 5, 4, 8, 3, 7 };
			int s= MaxArea(arry);
			Console.Write(s);
			Console.ReadKey();
		}

方法二: 双指针

等会写....

猜你喜欢

转载自blog.csdn.net/us2019/article/details/86304636