LeetCode Add and Search Word - Data structure design Trie

本文介绍了一种使用Trie树(字典树)的数据结构来实现单词字典的方法,该字典支持添加单词及搜索包含通配符'.'的单词。通过递归深度优先搜索实现了对带有通配符的字符串进行高效匹配。

思路:

根据hint应该用trie树,否则肯定超时。


java code:

public class WordDictionary {
    class TrieNode {
        boolean isLeaf;
        TrieNode[] children = new TrieNode[26];

        public TrieNode() {
        }
    }
    private TrieNode root = new TrieNode();
    // Adds a word into the data structure.
    public void addWord(String word) {
        TrieNode node = root;
        for(char c : word.toCharArray()) {
            if(node.children[c - 'a'] == null) {
                node.children[c - 'a'] = new TrieNode();
            }
            node = node.children[c - 'a'];
        }
        node.isLeaf = true;
    }

    // Returns if the word is in the data structure. A word could
    // contain the dot character '.' to represent any one letter.
    private boolean dfs(String word, TrieNode cur) {
        if(cur == null) return false;
        if(word.length() == 0) return cur.isLeaf;
        TrieNode[] children = cur.children;
        char c = word.charAt(0);
        if(c == '.') {
            for(TrieNode child : children) {
                if(child != null && dfs(word.substring(1), child)) {
                    return true;
                }
            }
            return false;
        }else if(children[c - 'a'] != null) {
            return dfs(word.substring(1), children[c - 'a']);
        }else {
            return false;
        }
    }
    public boolean search(String word) {
        return dfs(word, root);
    }    
}
内容概要:本文以一款电商类Android应用为案例,系统讲解了在Android Studio环境下进行性能优化的全过程。文章首先分析了常见的性能问题,如卡顿、内存泄漏和启动缓慢,并深入探讨其成因;随后介绍了Android Studio提供的三大性能分析工具——CPU Profiler、Memory Profiler和Network Profiler的使用方法;接着通过实际项目,详细展示了从代码、布局、内存到图片四个维度的具体优化措施,包括异步处理网络请求、算法优化、使用ConstraintLayout减少布局层级、修复内存泄漏、图片压缩与缓存等;最后通过启动时间、帧率和内存占用的数据对比,验证了优化效果显著,应用启动时间缩短60%,帧率提升至接近60fps,内存占用明显下降并趋于稳定。; 适合人群:具备一定Android开发经验,熟悉基本组件和Java/Kotlin语言,工作1-3年的移动端研发人员。; 使用场景及目标:①学习如何使用Android Studio内置性能工具定位卡顿、内存泄漏和启动慢等问题;②掌握从代码、布局、内存、图片等方面进行综合性能优化的实战方法;③提升应用用户体验,增强应用稳定性与竞争力。; 阅读建议:此资源以真实项目为背景,强调理论与实践结合,建议读者边阅读边动手复现文中提到的工具使用和优化代码,并结合自身项目进行性能检测与调优,深入理解每项优化背后的原理。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值