LeetCode 524. 通过删除字母匹配到字典里最长单词(动态规划) / 695. 岛屿的最大面积 / 54. 螺旋矩阵(背)

524. 通过删除字母匹配到字典里最长单词

2021.9.14 每日一题

题目描述

给你一个字符串 s 和一个字符串数组 dictionary 作为字典,找出并返回字典中最长的字符串,该字符串可以通过删除 s 中的某些字符得到。

如果答案不止一个,返回长度最长且字典序最小的字符串。如果答案不存在,则返回空字符串。

示例 1:

输入:s = “abpcplea”, dictionary = [“ale”,“apple”,“monkey”,“plea”]
输出:“apple”

示例 2:

输入:s = “abpcplea”, dictionary = [“a”,“b”,“c”]
输出:“a”

提示:

1 <= s.length <= 1000
1 <= dictionary.length <= 1000
1 <= dictionary[i].length <= 1000
s 和 dictionary[i] 仅由小写英文字母组成

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-word-in-dictionary-through-deleting
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路

长度降序,长度相同,按字典序排,然后双指针

class Solution {
    public String findLongestWord(String s, List<String> dictionary) {
        int l = s.length();
        int n = dictionary.size();
        //长度降序,可以将直接找到的第一个字符串返回,长度相同按字典序排
        Collections.sort(dictionary, (a, b) -> (a.length() == b.length() ? a.compareTo(b) : b.length() - a.length()));
        
        for(int i = 0; i < n; i++){
            String ss = dictionary.get(i);
            int m = ss.length();
            if(m > l)
                continue;
            int idx1 = 0;
            int idx2 = 0;
            while(idx1 < l && idx2 < m){
                if(s.charAt(idx1) == ss.charAt(idx2)){
                    idx1++;
                    idx2++;
                }else{
                    idx1++;
                }
            }
            if(idx2 == m)
                return ss;
        }
        return "";
    }
}

其实很容易有另一个想法,就是预处理字符串s,如果能迅速找到下一个位置的字符,就会加快处理速度
所以学习了一下这个动态规划
f[i][j]表示i位置能取到第一个j字符的下标

class Solution {
    public String findLongestWord(String s, List<String> dictionary) {
        int l = s.length();
        int n = dictionary.size();
        //动规,处理每个位置后面第一次出现其他字符的下标
        int[][] f = new int[l + 1][26];
        //初始化,最后一个位置所有字符不可能取到
        Arrays.fill(f[l], l);
        for(int i = l - 1; i >= 0; i--){
            for(int j = 0; j < 26; j++){
                if(s.charAt(i) == j + 'a'){
                    f[i][j] = i;
                }else{
                    f[i][j] = f[i + 1][j];
                }
            }
        }

        String res = "";
        for(String t : dictionary){
            int idx = 0;
            boolean flag = true;
            for(int i = 0; i < t.length(); i++){
                char c = t.charAt(i);
                if(f[idx][c - 'a'] == l){
                    flag = false;
                    break;
                }
                idx = f[idx][c - 'a'] + 1;
            }
            if(flag){
                if(t.length() > res.length() || (t.length() == res.length() && t.compareTo(res) < 0))
                    res = t;
            }
        }
        return res;

    }
}

695. 岛屿的最大面积

今天做了B站的笔试题,虽然赛码系统依旧非常坑,但是最大岛屿旋转矩阵我写不对???一瞬间想放弃了

题目描述

给定一个包含了一些 0 和 1 的非空二维数组 grid 。

一个 岛屿 是由一些相邻的 1 (代表土地) 构成的组合,这里的「相邻」要求两个 1 必须在水平或者竖直方向上相邻。你可以假设 grid 的四个边缘都被 0(代表水)包围着。

找到给定的二维数组中最大的岛屿面积。(如果没有岛屿,则返回面积为 0 。)

示例 1:

[[0,0,1,0,0,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,1,1,0,1,0,0,0,0,0,0,0,0],
[0,1,0,0,1,1,0,0,1,0,1,0,0],
[0,1,0,0,1,1,0,0,1,1,1,0,0],
[0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,0,0,0,0,0,0,1,1,0,0,0,0]]
对于上面这个给定矩阵应返回 6。注意答案不应该是 11 ,因为岛屿只能包含水平或垂直的四个方向的 1 。

示例 2:

[[0,0,0,0,0,0,0,0]]
对于上面这个给定的矩阵, 返回 0。

注意: 给定的矩阵grid 的长度和宽度都不超过 50。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/max-area-of-island
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路

class Solution {
    int m;
    int n;
    int[][] g;
    int max;
    int[][] dir = {{0,1},{0,-1},{1,0},{-1,0}};
    public int maxAreaOfIsland(int[][] grid) {
        m = grid.length;
        n = grid[0].length;
        g = grid;
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(g[i][j] == 1){
                    max = Math.max(max, dfs(i, j));
                }
            }
        }
        return max;
    }

    public int dfs(int i, int j){
        if(i < 0 || j < 0 || i >= m || j >= n){
            return 0;
        }
        if(g[i][j] == 0)
            return 0;
        int res = 0;
        g[i][j] = 0;
        for(int t = 0; t < 4; t++){
            int x = i + dir[t][0];
            int y = j + dir[t][1];
            res += dfs(x, y);
        }
        return res + 1;
    }
}

并查集捎带复习一下:

class Solution {

    public int maxAreaOfIsland(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        UnionFind uf = new UnionFind(m * n);
        boolean flag = false;
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                //和左面上面的合并,因为如果有一个点的话,为1,就不会去合并,所以需要特判一下
                if(grid[i][j] == 1){
                    flag = true;
                    if(i > 0 && grid[i - 1][j] == 1)
                        uf.union(i * n + j, (i - 1) * n + j);
                    if(j > 0 && grid[i][j - 1] == 1)
                        uf.union(i * n + j, i * n + j - 1);
                }
            }
        }
        if(flag)
            return uf.getmax() > 0 ? uf.getmax() : 1;
        else
            return 0;
    }

}

class UnionFind{
    int[] parents;
    int[] rank;
    int n;
    int[] size;
    int count;
    int max;
    public UnionFind(int n){
        this.n = n;
        parents = new int[n];
        rank = new int[n];
        size = new int[n];
        count = n;
        max = 0;
        for(int i = 0; i < n; i++){
            parents[i] = i;
            size[i] = 1;
            rank[i] = 1;
        }
    }

    public void union(int x, int y){
        int px = find(x);
        int py = find(y);
        if(px == py)
            return;
        if(rank[px] == rank[py]){
            parents[px] = py;
            rank[py]++;
            size[py] += size[px];
            max = Math.max(max, size[py]);
            return;
        }
        if(rank[px] > rank[py]){
            int temp = px;
            px = py;
            py = temp;
        }
        parents[px] = py;
        size[py] += size[px];
        max = Math.max(max, size[py]);
    }

    public int find(int x){
        if(parents[x] != x){
            parents[x] = find(parents[x]);
        }
        return parents[x];
    }

    public int getConnection(){
        return count;
    }

    public int getmax(){
        return max;
    }
}

54. 螺旋矩阵

题目描述

给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。

示例 1:

在这里插入图片描述
输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[1,2,3,6,9,8,7,4,5]

示例 2:

在这里插入图片描述
输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
输出:[1,2,3,4,8,12,11,10,9,5,6,7]

提示:

m == matrix.length
n == matrix[i].length
1 <= m, n <= 10
-100 <= matrix[i][j] <= 100

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/spiral-matrix
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路

这个螺旋矩阵的写法, 背下来!!!!

class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> res = new ArrayList<>();
        int m = matrix.length;
        int n = matrix[0].length;
        //定义四个方向
        int left = 0;
        int right = n - 1;
        int up = 0;
        int bottom = m - 1;
        //总共的数目
        int count = m * n;
        while(count >= 1){
            for(int i = left; i <= right && count >= 1; i++){
                res.add(matrix[up][i]);
                count--;
            }
            up++;
            for(int i = up; i <= bottom && count >= 1; i++){
                res.add(matrix[i][right]);
                count--;
            }
            right--;
            for(int i = right; i >= left && count >= 1; i--){
                res.add(matrix[bottom][i]);
                count--;
            }
            bottom--;
            for(int i = bottom; i >= up && count >= 1; i--){
                res.add(matrix[i][left]);
                count--;
            }
            left++;
        }
        return res;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值