【Weekly】No.184

C_01 数组中的字符串匹配

给你一个字符串数组 words ,数组中的每个字符串都可以看作是一个单词。请你按 任意 顺序返回 words 中是其他单词的子字符串的所有单词。

如果你可以删除 words[j] 最左侧和/或最右侧的若干字符得到 word[i] ,那么字符串 words[i] 就是 words[j] 的一个子字符串。

输入:words = ["mass","as","hero","superhero"]
输出:["as","hero"]
解释:"as" 是 "mass" 的子字符串,"hero" 是 "superhero" 的子字符串。
["hero","as"] 也是有效的答案。

方法一:暴力

public List<String> stringMatching(String[] words) {
    LinkedList<String> list = new LinkedList<>();
    for (int i = 0; i < words.length; i++) {
        for (int j = 0; j < words.length; j++) {
            if (i == j)
                continue;
            if (words[i].contains(words[j]) && !list.contains(words[j])) 
                list.add(words[j]);
        }
    }
    return list;
}

复杂度分析

  • 时间复杂度: O ( ) O()
  • 空间复杂度: O ( ) O()

B_02 查询带键的排列

给你一个待查数组 queries ,数组中的元素为 1 到 m 之间的正整数。 请你根据以下规则处理所有待查项 queries[i](从 i=0 到 i=queries.length-1):

  • 一开始,排列 P=[1,2,3,…,m]。
  • 对于当前的 i ,请你找出待查项 queries[i] 在排列 P 中的位置(下标从 0 开始),然后将其从原位置移动到排列 P 的起始位置(即下标为 0 处)。注意, queries[i] 在 P 中的位置就是 queries[i] 的查询结果。

请你以数组形式返回待查数组 queries 的查询结果。

  • 1 <= m <= 10^3
    1 <= queries.length <= m
    1 <= queries[i] <= m
输入:queries = [3,1,2,1], m = 5
输出:[2,1,2,1] 
解释:待查数组 queries 处理如下:
对于 i=0: queries[i]=3, P=[1,2,3,4,5], 3 在 P 中的位置是 2,接着我们把 3 移动到 P 的起始位置,得到 P=[3,1,2,4,5] 。
对于 i=1: queries[i]=1, P=[3,1,2,4,5], 1 在 P 中的位置是 1,接着我们把 1 移动到 P 的起始位置,得到 P=[1,3,2,4,5] 。 
对于 i=2: queries[i]=2, P=[1,3,2,4,5], 2 在 P 中的位置是 2,接着我们把 2 移动到 P 的起始位置,得到 P=[2,1,3,4,5] 。
对于 i=3: queries[i]=1, P=[2,1,3,4,5], 1 在 P 中的位置是 1,接着我们把 1 移动到 P 的起始位置,得到 P=[1,2,3,4,5] 。 
因此,返回的结果数组为 [2,1,2,1]

方法一:模拟

public int[] processQueries(int[] queries, int m) {
    ArrayList<Integer> nums = new ArrayList<>();
    for (int i = 1; i <= m; i++) {
        nums.add(i);
    }
    LinkedList<Integer> res = new LinkedList<Integer>();
    for (int q : queries) {
        int idx = nums.indexOf(q);
        res.add(idx);
        nums.remove(idx);
        nums.add(0, q);
    }
    int p = 0;
    int[] arr = new int[queries.length];
    for (int i : res) {
        arr[p++] = i;
    }
    return arr;
}

复杂度分析

  • 时间复杂度: O ( n ) O(n)
  • 空间复杂度: O ( n ) O(n)

B_03 HTML 实体解析器

「HTML 实体解析器」 是一种特殊的解析器,它将 HTML 代码作为输入,并用字符本身替换掉所有这些特殊的字符实体。

HTML 里这些特殊字符和它们对应的字符实体包括:

  • 双引号:字符实体为 " ,对应的字符是 " 。
    单引号:字符实体为 ’ ,对应的字符是 ’ 。
    与符号:字符实体为 & ,对应对的字符是 & 。
    大于号:字符实体为 > ,对应的字符是 > 。
    小于号:字符实体为 < ,对应的字符是 < 。
    斜线号:字符实体为 ⁄ ,对应的字符是 / 。
    给你输入字符串 text ,请你实现一个 HTML 实体解析器,返回解析器解析后的结果。

方法一:字符串的 replace

public String entityParser(String text) {
    String res = text;
    res = res.replaceAll("&quot;", "\"");
    res = res.replaceAll("&apos;", "'");
    res = res.replaceAll("&amp;", "&");
    res = res.replaceAll("&gt;", ">");
    res = res.replaceAll("&lt;", "<");
    res = res.replaceAll("&frasl;", "/");
    return res;
}  

复杂度分析

  • 时间复杂度: O ( n ) O(n)
  • 空间复杂度: O ( 1 ) O(1)

A_04 给 N x 3 网格图涂色的方案数(没 AC)

在这里插入图片描述

输入:n = 2
输出:54

输入:n = 3
输出:246

示例 4:
输入:n = 7
输出:106494

示例 5:
输入:n = 5000
输出:30228214

提示:

  • n == grid.length
    grid[i].length == 3
    1 <= n <= 5000

方法一:


复杂度分析

  • 时间复杂度: O ( ) O()
  • 空间复杂度: O ( ) O()

发布了714 篇原创文章 · 获赞 199 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_43539599/article/details/105466164
今日推荐