120. Triangle

给出一个三角形状的整数list,从第一行开始向下移动,每次只能向下一行相邻的数移动,要求求出从第一行

到最后一行所经过的数字的和最小的方法。

1. 自顶向下计算

public int minimumTotal(List<List<Integer>> triangle)
	{
		int row = triangle.size(); // 行数
		if (row == 0) // triangle长度为0
			return 0;
		
		int[][] res = new int[row][row]; // 记录从第一行到每个点的最小值
		int minSum = Integer.MAX_VALUE; // 借助于求出最小值
		// 先处理第一行
		List<Integer> list0 = triangle.get(0);
		//if (list0.size() == 0)
			//return 0;
		if (row == 1)
			return list0.get(0);
		
		res[0][0] = list0.get(0);
		// 再处理余下的几行
		for (int i=1; i<row; i++)
		{
			List<Integer> list = triangle.get(i);
			for (int j=0; j<list.size(); j++)
			{
				// 处理第i行的第一个位置的元素
				if (j == 0) 
				{
					res[i][j] = res[i-1][j] + list.get(j);
				}
				// 处理第i行的最后一个位置的元素
				else if (j == list.size() - 1)
				{
					res[i][j] = res[i-1][j-1] + list.get(j);
				}
				// 处理第i行中间位置的元组
				else 
				{
					res[i][j] = Math.min(res[i-1][j], res[i-1][j-1]) + list.get(j);
				}
				
				// 如果当前行是最后一行
				if (i == row - 1) 
				{
					if (minSum > res[i][j])
						minSum = res[i][j];
				}
			}
		}
		
		return minSum;
	}

2. 自底向上计算

	public int minimumTotal(List<List<Integer>> triangle)
	{
		int m = triangle.size(); // 得到二维列表的大小
		for (int i = m - 2; i >= 0; i--) // i从倒数第2层开始循环
		{
			for (int j = 0; j < i + 1; ++j) // 第i层的下一层
			{
				int old = triangle.get(i).get(j);
				triangle.get(i).set(j, old + Math.min(triangle.get(i + 1).get(j), triangle.get(i + 1).get(j + 1)));
			}
		}
		return triangle.get(0).get(0); // 返回第一个元素
	}

参考博文:https://www.cnblogs.com/271934Liao/p/6919843.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Serpinski's Triangle and Carpet are two famous fractal patterns named after the Polish mathematician Wacław Sierpiński. These patterns are created through a recursive process of dividing shapes into smaller copies of themselves. 1. Serpinski's Triangle: Serpinski's Triangle is a fractal pattern that starts with an equilateral triangle. In each iteration, the triangle is divided into four smaller triangles by connecting the midpoints of its sides. The central triangle is then removed, and the process is repeated for the remaining three triangles. This recursive division continues indefinitely, creating a pattern of smaller triangles within the original triangle. 2. Serpinski's Carpet: Serpinski's Carpet is a fractal pattern that starts with a square. In each iteration, the square is divided into nine smaller squares by removing the central square and dividing the remaining eight squares into nine equal-sized squares. The process is then repeated for each of the remaining eight squares. This recursive division continues indefinitely, creating a pattern of smaller squares within the original square. Both Serpinski's Triangle and Carpet exhibit self-similarity, meaning that they contain smaller copies of themselves at different scales. These fractal patterns have been widely studied and appreciated for their intricate and visually appealing structures. If you want to generate Serpinski's Triangle or Carpet using Python, you can use recursion and graphical libraries like Turtle or Matplotlib to draw the patterns. Here's an example code snippet for generating Serpinski's Triangle using Turtle: ```python import turtle def draw_triangle(length, depth): if depth == 0: for _ in range(3): turtle.forward(length) turtle.left(120) else: draw_triangle(length / 2, depth - 1) turtle.forward(length / 2) draw_triangle(length / 2, depth - 1) turtle.backward(length / 2) turtle.left(60) turtle.forward(length / 2) turtle.right(60) draw_triangle(length / 2, depth - 1) turtle.left(60) turtle.backward(length / 2) turtle.right(60) # Set up the turtle turtle.speed(0) turtle.penup() turtle.goto(-200, -200) turtle.pendown() # Draw Serpinski's Triangle draw_triangle(400, 4) # Hide the turtle turtle.hideturtle() # Keep the window open turtle.done() ``` This code will generate Serpinski's Triangle with a depth of 4. You can adjust the depth parameter to control the level of detail in the pattern.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值