301. Remove Invalid Parentheses
BFS实现的,但是会出现一个问题,即也会把有效长度-1的部分string放入 queue,为什么这样也可以正确呢?
是因为有效长度的括号数量是偶数,减掉一个的是奇数,一定无效,由此还可以优化代码,这一层的全部不用判断是否有效。
208. Implement Trie (Prefix Tree)
其中有段代码
for(int i =0;i<word.length();i++){
char it= word.charAt(i);
now = now.children[it-'a'];
if(now == null){
now = new TrieNode(it);
}
}
问题出在哪呢?正确代码
for(int i =0;i<word.length();i++){
char it= word.charAt(i);
if(now.children[it-'a'] == null){
now.children[it-'a'] = new TrieNode(it);
}
now = now.children[it-'a'];
}