61-65题 题解

本文介绍了LeetCode中关于列表旋转和路径规划的算法问题,包括将列表向右旋转指定位置,计算网格中从左上角到右下角的不同路径数量,考虑障碍物时的路径计数,以及找到网格中最小路径和的方法。同时,讨论了一个验证字符串是否为数字的题目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

61Rotate List

Given a list, rotate the list to the right by k places, where k is non-negative.


Example:

Given 1->2->3->4->5->NULL and k = 2,

return 4->5->1->2->3->NULL.
//如果k大于链表长度时,应该旋转 k%len
//一个快指针,一个慢指针,快的指针比慢的先走k个,这样当快的指针走到NULL时,慢的刚好是倒数第k个
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* rotateRight(ListNode* head, int k) {
        if(!head || !head->next) return head;
        ListNode *p, *q;
        p = q = head;
        int len = 0;
        while(p)
        {
            p = p->next;
            len++;
        }
        k %= len;
        if(k == 0) return head;
        p = head;
        while(p && k--)
        {
            p = p->next;
        }
        if(!p) return head;
        while(p->next)
        {
            p = p->next;
            q = q->next;
        }
        ListNode *cur = q->next;
        q->next = NULL;
        p->next = head;
        return cur;
    }
};
62Unique 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
class Solution {
public:
    int vis[101][101];
    int dfs(int i, int j, int m, int n)
    {
        if(vis[i][j]) return vis[i][j];
        if(i == m && n == j) return 1;
        int a, b;
        a = b = 0;

        if(i < m) a = vis[i+1][j] = dfs(i+1, j, m, n);
        if(j < n) b = vis[i][j+1] = dfs(i, j+1, m, n);
        return a + b;
    }
    int uniquePaths(int m, int n) {
        memset(vis, 0, sizeof(vis));
        return dfs(1, 1, m, n);
    }
};


63Unique Paths II

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.

//同上一题
class Solution {
public:
    int vis[101][101];
    int dfs(int i, int j, int m, int n, vector<vector<int>>& obstacleGrid)
    {
        if(vis[i][j] != -1) return vis[i][j];
        if(i == m && n == j) return 1;
        int a, b;
        a = b = 0;

        if(i < m && !obstacleGrid[i+1][j]) a = vis[i+1][j] = dfs(i+1, j, m, n, obstacleGrid);
        if(j < n && !obstacleGrid[i][j+1]) b = vis[i][j+1] = dfs(i, j+1, m, n, obstacleGrid);
        return a + b;
    }
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
        if(obstacleGrid.size() == 0) return 0;
        memset(vis, -1, sizeof(vis));
        int m = obstacleGrid.size()-1;
        int n = obstacleGrid[0].size()-1;
        if(obstacleGrid[m][n] || obstacleGrid[0][0]) return 0;
        return dfs(0, 0, m, n, obstacleGrid);
    }
};


64Minimum Path Sum

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.

Note: You can only move either down or right at any point in time.

Example 1:

[[1,3,1],
 [1,5,1],
 [4,2,1]]
Given the above grid map, return 7. Because the path 1→3→1→1→1 minimizes the sum.

//同上一题,也可以DP
class Solution {
public:
    int vis[509][509];
    int dfs(int i, int j, int m, int n, vector<vector<int>>& grid)
    {
        if(vis[i][j] != 0x3f3f3f3f) return vis[i][j];
        if(i == m && n == j) return grid[m][n];
        if(i < m)
        {
            int k = dfs(i+1, j, m, n, grid);
            vis[i][j] = min(vis[i][j], k+grid[i][j]);
        }
        if(j < n)
        {
            int k = dfs(i, j+1, m, n, grid);
            vis[i][j] = min(vis[i][j], k+grid[i][j]);
        }
        return vis[i][j];
    }
    int minPathSum(vector<vector<int>>& grid) {
        if(grid.size() == 0) return 0;
        memset(vis, 0x3f, sizeof(vis));
        return dfs(0, 0, grid.size()-1, grid[0].size()-1, grid);
    }
};
65Valid Number

Validate if a given string is numeric.

Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true

Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.

Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button  to reset your code definition.

//WA了十万发,终于给A了,需要判断的地方有很多
// . + - ' ' e E等
class Solution {
public:
    bool isNumber(string s) {
        int len = s.length();

        int falg = 0, i = 0;
        while(i < len && s[i] == ' ') i++;
        if(len == i) return false;
        while(i < len)
        {
            if(isdigit(s[i])) i++;
            else if((s[i] == 'e' || s[i] == 'E') && falg != 2)
            {
                if(i == 0 || s[i-1] == ' ') return false;
                if(((i+1 < len) && isdigit(s[i+1])) || ((i+2 < len)&&((s[i+1] == '-'||s[i+1]=='+') && isdigit(s[i+2])))) i += 2;
                else return false;
                falg = 2;
            }
            else if(s[i] == '-' || s[i] == '+')
            {
                if(i - 1 >= 0 && isdigit(s[i-1])) return false;
                if((i+1 < len && isdigit(s[i+1])) || (i+2 < len && s[i+1] == '.' && isdigit(s[i+2]))) i += 2;
                else return false;
            }
            else if(s[i] == '.' && !falg)
            {
                i++;
                if(i < len && isdigit(s[i])) i++;
                else if(i - 2 >= 0 && isdigit(s[i-2])) ;
                else return false;
                falg = 1;
            }
            else if(s[i] == ' ')
            {
                while(i < len && s[i] == ' ') i++;
                if(i == len) return true;
                return false;
            }
            else return false;
        }
        return true;
    }
};




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值