Java&C++题解与拓展——leetcode890.查找和替换模式【么的新知识】

每日一题做题记录,参考官方和三叶的题解

题目要求

在这里插入图片描述

思路:哈希表模拟

  • 用哈希表 m a p map map存放当前单词与模板之间的映射关系,为了避免重复映射,维护一个 v i s vis vis表示已遍历过;
    • 字符集数量小(26个),所以用数组替代了。

Java

class Solution {
    public List<String> findAndReplacePattern(String[] words, String pattern) {
        List<String> res = new ArrayList<>();
        int[] map = new int[26], vis = new int[26];
        for(String w : words) {
            Arrays.fill(map, -1);
            Arrays.fill(vis, 0);
            boolean match = true;
            for(int i = 0; i < pattern.length() && match; i++) {
                int cw = w.charAt(i) - 'a', cp = pattern.charAt(i) - 'a';
                if(map[cw] == -1 && vis[cp] == 0) {
                    map[cw] = cp; // cp映射为cw
                    vis[cp] = 1;
                }
                else if(map[cw] != cp)
                    match = false;
            }
            if(match)
                res.add(w);
        }
        return res;
    }
}
  • 时间复杂度: O ( ∑ i = 0 n l e n ( w s [ i ] ) + n × C ) O(\sum^n_{i=0}len(ws[i])+n\times C) O(i=0nlen(ws[i])+n×C),其中 C C C为字符集大小,即26.
  • 空间复杂度: O ( C ) O(C) O(C)

C++

class Solution {
public:
    vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
        vector<string> res;
        int map[26], vis[26];
        for(string w : words) {
            memset(map, -1, sizeof(map));
            memset(vis, 0, sizeof(vis));
            bool match = true;
            for(int i = 0; i < pattern.length() && match; i++) {
                int cw = w[i] - 'a', cp = pattern[i] - 'a';
                if(map[cw] == -1 && vis[cp] == 0) {
                    map[cw] = cp; // cp映射为cw
                    vis[cp] = 1;
                }
                else if(map[cw] != cp)
                    match = false;
            }
            if(match)
                res.push_back(w);
        }
        return res;
    }
};
  • 时间复杂度: O ( ∑ i = 0 n l e n ( w s [ i ] ) + n × C ) O(\sum^n_{i=0}len(ws[i])+n\times C) O(i=0nlen(ws[i])+n×C),其中 C C C为字符集大小,即26.
  • 空间复杂度: O ( C ) O(C) O(C)

Rust

  • usize类型里似乎不能识别负号,所以就搞了个99替代。
impl Solution {
    pub fn find_and_replace_pattern(words: Vec<String>, pattern: String) -> Vec<String> {
        let mut res = Vec::new();
        for w in words {
            let(mut map, mut vis, mut MATCH) = (vec![0; 26], vec![0; 26], true);
            for i in 0..26 {
                map[i] = 99 as usize;
                vis[i] = 0 as usize;
            }
            for i in 0..w.len() {
                let cw = (w.as_bytes()[i] - 'a' as u8) as usize;
                let cp = (pattern.as_bytes()[i] - 'a' as u8) as usize;
                if map[cw] == 99 && vis[cp] == 0 {
                    map[cw] = cp; // cp 映射为cw
                    vis[cp] = 1;
                }
                else if map[cw] != cp {
                    MATCH = false;
                }
            }
            if MATCH {
                res.push(w);
            }
        }
        res
    }
}
  • 时间复杂度: O ( ∑ i = 0 n l e n ( w s [ i ] ) + n × C ) O(\sum^n_{i=0}len(ws[i])+n\times C) O(i=0nlen(ws[i])+n×C),其中 C C C为字符集大小,即26.
  • 空间复杂度: O ( C ) O(C) O(C)

总结

模拟题么的好总结……

学习了 R u s t Rust Rust的usize、u8乱七八糟的类型。


欢迎指正与讨论!
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值