Triangle

triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.

For example, given the following triangle

[
     [2],
    [3,4],
   [6,5,7],
  [4,1,8,3]
]

The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).

Note:
Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.

Solve the Problem on Leetcode


class Solution {
public:
    int minimumTotal(vector<vector<int>>& triangle) {
 //从下向上
        int n=triangle.size();
        if(n==0)  return 0;
        vector<int> res(n,0);
        for(int i=0;i<n;i++)
        res[i]=triangle[n-1][i];
       // if(n==1) return res[0];
        for(int i=n-2;i>=0;i--)
        {
            for(int j=0;j<=i;j++)
            {
               // res[j]=(res[j]<res[j+1])?res[j]:res[j+1]+triangle[i][j];
               if(res[j]<res[j+1])
               res[j]=res[j]+triangle[i][j];
               else
               res[j]=res[j+1]+triangle[i][j];
            }
        }
        return res[0];
        
    }
    
};


在计算机科学与数学中,异或(XOR)操作和三角形性质之间的关系主要体现在集合运算的对称差以及图像处理中的几何变换等方面。以下从两个角度分析这种联系: ### 1. 集合论中的对称差与三角不等式 在集合论中,XOR 操作可视为两个集合的**对称差**(Symmetric Difference),记作 $ A \triangle B $,其定义为: $$ A \triangle B = (A \cup B) - (A \cap B) $$ 即仅属于其中一个集合而不同时属于两者的元素集合。此操作具有如下性质: - **结合律**:$ (A \triangle B) \triangle C = A \triangle (B \triangle C) $ - **交换律**:$ A \triangle B = B \triangle A $ 此外,对称差满足**三角不等式**(Triangle Inequality)[^1],即: $$ A \triangle C \subseteq (A \triangle B) \cup (B \triangle C) $$ 这表明若集合 $ A $ 与 $ C $ 差别较大,则必可通过 $ A $ 与 $ B $、$ B $ 与 $ C $ 的差异组合得到。 ### 2. 图像处理中的 XOR 模式与几何变换 在图像处理领域,XOR 操作常用于实现图形的**动态绘制与擦除**,例如使用 GDI(Graphics Device Interface)进行位图合成时[^4]。具体应用场景包括: - **鼠标拖动选择框**:通过 XOR 模式绘制临时矩形,避免背景图像被覆盖。 - **动画效果**:实现对象的移动而不重绘整个场景。 代码示例(C# 中使用 GDI 实现 XOR 模式贴图): ```csharp // 创建兼容设备上下文 IntPtr hdcMem = CreateCompatibleDC(hdc); // 选择位图到设备上下文中 SelectObject(hdcMem, hBitmap); // 使用 BitBlt 进行 XOR 模式贴图 BitBlt(hdc, x, y, width, height, hdcMem, 0, 0, ROP_XORPEN); ``` 在计算机视觉中,XOR 操作也可用于检测图像间的差异,例如通过比较两幅图像的像素值来识别变化区域。 ### 3. 算法设计中的 XOR 技巧 XOR 在算法设计中也扮演重要角色,例如: - **交换两个变量的值**而无需额外空间: ```python a ^= b b ^= a a ^= b ``` - **查找唯一出现的数字**:若数组中所有数字均出现两次,只有一个例外,则 XOR 所有数即可得该数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值