leetcode Unique Paths && Unique Paths

Desciption:

Unique Paths

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

How many possible unique paths are there?


Above is a 3 x 7 grid. How many possible unique paths are there?

Note: m and n will be at most 100.


DP方程:

res[i][j] =      1        i = 0 || j = 0

   res[i-1][j] + res[i][j-1]   i ,j >0



class Solution {
public:
    int uniquePaths(int m, int n) {
        vector<vector<int>> result(m, vector<int>(n,0));
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if ( i > 0 && j > 0)
                    result[i][j] = result[i-1][j] + result[i][j-1];
                if (i == 0 || j == 0) 
                    result[i][j] = 1;

            }
        }
        return result[m - 1][n - 1];
    }
};




Desciption:

Unique Paths 2;

Follow up for "Unique Paths":

Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

For example,

There is one obstacle in the middle of a 3x3 grid as illustrated below.

[
  [0,0,0],
  [0,1,0],
  [0,0,0]
]

The total number of unique paths is 2.

Note: m and n will be at most 100.



注意到 DP方程还是一样,但是当 矩阵[M][N] = 1时, res[M][N] = 0;


class Solution {
public:
    int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
        int m = obstacleGrid.size() , n = obstacleGrid[0].size();
        vector<vector<int>> dp(m+1,vector<int>(n+1,0));
        dp[0][1] = 1;
        for(int i = 1 ; i <= m ; ++i)
            for(int j = 1 ; j <= n ; ++j)
                if(!obstacleGrid[i-1][j-1])
                    dp[i][j] = dp[i-1][j]+dp[i][j-1];
        return dp[m][n];
    }
};



你可以使用 Vue Router 的导航守卫来实现外链跳转时携带 token 但不显示在地址栏中。下面是一种可能的解决方案: 1. 首先,确保你已经安装了 Vue Router,并在项目中引入它。 2. 在路由文件中,定义一个全局前置守卫(global before guard),用来拦截所有的外链跳转。 ```javascript import router from './router' router.beforeEach((to, from, next) => { // 判断是否是外链跳转 if (to.meta.externalLink) { // 获取 token const token = 'your_token_here' // 创建一个隐藏的表单,用于提交 token const form = document.createElement('form') form.action = to.path form.method = 'post' // 创建一个隐藏的输入框,存放 token const tokenInput = document.createElement('input') tokenInput.type = 'hidden' tokenInput.name = 'token' tokenInput.value = token // 将输入框添加到表单中 form.appendChild(tokenInput) // 将表单添加到页面中 document.body.appendChild(form) // 提交表单 form.submit() } else { next() } }) ``` 3. 在定义路由时,为外链跳转的路由添加一个 meta 属性,用来标识它是外链跳转。 ```javascript const routes = [ { path: '/external', name: 'ExternalLink', component: ExternalLink, meta: { externalLink: true } }, // 其他路由... ] const router = createRouter({ history: createWebHistory(), routes }) export default router ``` 这样,当用户点击一个带有 `externalLink` meta 属性的链接时,会触发全局前置守卫,创建一个隐藏的表单,将 token 作为参数提交,然后页面会进行跳转,但地址栏中并不会显示 token。 请注意,这只是一种解决方案,具体实现可能因你的项目结构和需求而有所不同。你需要根据实际情况进行调整和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值