Skip to content

Commit 53ee678

Browse files
author
Botao Xiao
committed
[Function add]:1. Add leetcode solutions with tag list.
1 parent b0bec36 commit 53ee678

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed

leetcode/450. Delete Node in a BST.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
## 450. Delete Node in a BST
2+
3+
### Question
4+
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.
5+
6+
Basically, the deletion can be divided into two stages:
7+
8+
Search for a node to remove.
9+
If the node is found, delete the node.
10+
Note: Time complexity should be O(height of tree).
11+
12+
```
13+
Example:
14+
15+
root = [5,3,6,2,4,null,7]
16+
key = 3
17+
18+
5
19+
/ \
20+
3 6
21+
/ \ \
22+
2 4 7
23+
24+
Given key to delete is 3. So we find the node with value 3 and delete it.
25+
26+
One valid answer is [5,4,6,2,null,null,7], shown in the following BST.
27+
28+
5
29+
/ \
30+
4 6
31+
/ \
32+
2 7
33+
34+
Another valid answer is [5,2,6,null,4,null,7].
35+
36+
5
37+
/ \
38+
2 6
39+
\ \
40+
4 7
41+
```
42+
43+
### Solution
44+
* Method 1: recursion
45+
![Imgur](https://i.imgur.com/rShtr1p.png)
46+
```Java
47+
/**
48+
* Definition for a binary tree node.
49+
* public class TreeNode {
50+
* int val;
51+
* TreeNode left;
52+
* TreeNode right;
53+
* TreeNode(int x) { val = x; }
54+
* }
55+
*/
56+
class Solution {
57+
public TreeNode deleteNode(TreeNode root, int key) {
58+
if(root == null) return root;
59+
if(key < root.val) root.left = deleteNode(root.left, key);
60+
else if(key > root.val) root.right = deleteNode(root.right, key);
61+
else{
62+
TreeNode res = null;
63+
if(root.left == null) res = root.right;
64+
else if(root.right == null) res = root.left;
65+
else{ // both left and right are not null.
66+
TreeNode parent = root;
67+
res = root.right;
68+
while(res.left != null){
69+
parent = res;
70+
res = res.left;
71+
}
72+
if(parent != root){
73+
parent.left = res.right;
74+
res.right = root.right;
75+
}
76+
res.left = root.left;
77+
}
78+
return res;
79+
}
80+
return root;
81+
}
82+
}
83+
```
84+
85+
### Reference
86+
1. [花花酱 LeetCode 450. Delete Node in a BST](http://zxi.mytechroad.com/blog/tree/leetcode-450-delete-node-in-a-bst/)

leetcode/819. Most Common Word.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
## 819. Most Common Word
2+
3+
### Question
4+
Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words. It is guaranteed there is at least one word that isn't banned, and that the answer is unique.
5+
6+
Words in the list of banned words are given in lowercase, and free of punctuation. Words in the paragraph are not case sensitive. The answer is in lowercase.
7+
8+
```
9+
Example:
10+
11+
Input:
12+
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
13+
banned = ["hit"]
14+
Output: "ball"
15+
Explanation:
16+
"hit" occurs 3 times, but it is a banned word.
17+
"ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph.
18+
Note that words in the paragraph are not case sensitive,
19+
that punctuation is ignored (even if adjacent to words, such as "ball,"),
20+
and that "hit" isn't the answer even though it occurs more because it is banned.
21+
22+
23+
Note:
24+
25+
1 <= paragraph.length <= 1000.
26+
0 <= banned.length <= 100.
27+
1 <= banned[i].length <= 10.
28+
The answer is unique, and written in lowercase (even if its occurrences in paragraph may have uppercase symbols, and even if it is a proper noun.)
29+
paragraph only consists of letters, spaces, or the punctuation symbols !?',;.
30+
There are no hyphens or hyphenated words.
31+
Words only consist of letters, never apostrophes or other punctuation symbols.
32+
```
33+
34+
### Solution
35+
* Method 1: HashMap
36+
```Java
37+
class Solution {
38+
public String mostCommonWord(String paragraph, String[] banned) {
39+
paragrap
40+
h = paragraph.replaceAll("[\\!\\?',;\\.]", " ");
41+
String[] tokens = paragraph.toLowerCase().split(" ");
42+
Map<String, Integer> map = new HashMap<>();
43+
int max = 0;
44+
Set<String> ban = new HashSet<>();
45+
for(String word: banned) ban.add(word);
46+
for(String token : tokens){
47+
token = token.trim();
48+
if(ban.contains(token) || token.length() == 0) continue;
49+
System.out.println(token);
50+
int occur = map.containsKey(token) ? map.get(token): 0;
51+
map.put(token, occur + 1);
52+
max = Math.max(max, occur + 1);
53+
}
54+
for(Map.Entry<String, Integer> entry : map.entrySet()){
55+
if(entry.getValue() == max) return entry.getKey();
56+
}
57+
return "";
58+
}
59+
}
60+
```
61+
62+
* Method 2: Use StringBuilder, O(n)
63+
```Java
64+
class Solution {
65+
public String mostCommonWord(String paragraph, String[] banned) {
66+
paragraph += ".";
67+
Set<String> set = new HashSet<>();
68+
for(String ban : banned) set.add(ban);
69+
StringBuilder sb = new StringBuilder();
70+
Map<String, Integer> map = new HashMap<>();
71+
int max = 0;
72+
String res = "";
73+
for(char c : paragraph.toCharArray()){
74+
if(Character.isLetter(c)){
75+
sb.append(Character.toLowerCase(c));
76+
}else if(sb.length() > 0){
77+
String token = sb.toString();
78+
if(!set.contains(token)){
79+
int occur = map.getOrDefault(token, 0);
80+
if(occur + 1 > max){
81+
max = occur + 1;
82+
res = token;
83+
}
84+
map.put(token, occur + 1);
85+
}
86+
sb = new StringBuilder();
87+
}
88+
}
89+
return res;
90+
}
91+
}
92+
```

0 commit comments

Comments
 (0)