Skip to content

Commit 1bc4533

Browse files
author
Botao Xiao
committed
[Function add]:1. Add leetcode solutions with tag amazon.
1 parent 6c9c785 commit 1bc4533

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
## 617. Merge Two Binary Trees
2+
3+
### Question
4+
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.
5+
6+
You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree.
7+
8+
```
9+
Example 1:
10+
11+
Input:
12+
Tree 1 Tree 2
13+
1 2
14+
/ \ / \
15+
3 2 1 3
16+
/ \ \
17+
5 4 7
18+
Output:
19+
Merged tree:
20+
3
21+
/ \
22+
4 5
23+
/ \ \
24+
5 4 7
25+
```
26+
27+
Note: The merging process must start from the root nodes of both trees.
28+
29+
### Solution
30+
* Method 1: Recursion
31+
```Java
32+
/**
33+
* Definition for a binary tree node.
34+
* public class TreeNode {
35+
* int val;
36+
* TreeNode left;
37+
* TreeNode right;
38+
* TreeNode(int x) { val = x; }
39+
* }
40+
*/
41+
class Solution {
42+
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
43+
if(t1 != null && t2 != null){
44+
t1.val = t1.val + t2.val;
45+
t1.left = mergeTrees(t1.left, t2.left);
46+
t1.right = mergeTrees(t1.right, t2.right);
47+
return t1;
48+
}else if(t1 != null || t2 != null){
49+
return t1 != null ? t1: t2;
50+
}else{
51+
return null;
52+
}
53+
}
54+
}
55+
```

leetcode/763. Partition Labels.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## 763. Partition Labels
2+
3+
### Question
4+
A string S of lowercase letters is given. We want to partition this string into as many parts as possible so that each letter appears in at most one part, and return a list of integers representing the size of these parts.
5+
6+
```
7+
Example 1:
8+
Input: S = "ababcbacadefegdehijhklij"
9+
Output: [9,7,8]
10+
Explanation:
11+
The partition is "ababcbaca", "defegde", "hijhklij".
12+
This is a partition so that each letter appears in at most one part.
13+
A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits S into less parts.
14+
```
15+
16+
Note:
17+
1. S will have length in range [1, 500].
18+
2. S will consist of lowercase letters ('a' to 'z') only.
19+
20+
### Solution
21+
* Method 1: Greedy
22+
```Java
23+
class Solution {
24+
public List<Integer> partitionLabels(String S) {
25+
int len = S.length();
26+
char[] arr = S.toCharArray();
27+
int cur = 0, last = 0, temp = 0, count = 0;
28+
Map<Character, Integer> map = findLast(arr);
29+
List<Integer> result = new ArrayList<>();
30+
while(cur < len){
31+
last = map.get(arr[cur]);
32+
temp = cur;
33+
while(temp < last){
34+
last = Math.max(last, map.get(arr[++temp]));
35+
}
36+
result.add(last - cur + 1);
37+
cur = last + 1;
38+
}
39+
return result;
40+
}
41+
private Map<Character, Integer> findLast(char[] arr){
42+
Map<Character, Integer> map = new HashMap<>();
43+
for(int i = arr.length - 1; i >= 0; i--){
44+
if(map.containsKey(arr[i])) continue;
45+
map.put(arr[i], i);
46+
}
47+
return map;
48+
}
49+
}
50+
```
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
## 929. Unique Email Addresses
2+
3+
### Question
4+
Every email consists of a local name and a domain name, separated by the @ sign.
5+
6+
For example, in [email protected], alice is the local name, and leetcode.com is the domain name.
7+
8+
Besides lowercase letters, these emails may contain '.'s or '+'s.
9+
10+
If you add periods ('.') between some characters in the local name part of an email address, mail sent there will be forwarded to the same address without dots in the local name. For example, "[email protected]" and "[email protected]" forward to the same email address. (Note that this rule does not apply for domain names.)
11+
12+
If you add a plus ('+') in the local name, everything after the first plus sign will be ignored. This allows certain emails to be filtered, for example [email protected] will be forwarded to [email protected]. (Again, this rule does not apply for domain names.)
13+
14+
It is possible to use both of these rules at the same time.
15+
16+
Given a list of emails, we send one email to each address in the list. How many different addresses actually receive mails?
17+
18+
19+
```
20+
Example 1:
21+
22+
23+
Output: 2
24+
Explanation: "[email protected]" and "[email protected]" actually receive mails
25+
```
26+
27+
Note:
28+
1. 1 <= emails[i].length <= 100
29+
2. 1 <= emails.length <= 100
30+
3. Each emails[i] contains exactly one '@' character.
31+
4. All local and domain names are non-empty.
32+
5. Local names do not start with a '+' character.
33+
34+
### Solution
35+
* Method 1: String
36+
```Java
37+
class Solution {
38+
public int numUniqueEmails(String[] emails) {
39+
Set<String> set = new HashSet<>();
40+
for(String email : emails){
41+
int index = email.indexOf('@');
42+
String local = email.substring(0, index);
43+
String domin = email.substring(index + 1);
44+
local = local.replace(".", "");
45+
index = local.indexOf("+");
46+
if(index == -1){
47+
set.add(local + "@" + domin);
48+
}else{
49+
set.add(local.substring(0, index) + "@" + domin);
50+
}
51+
}
52+
return set.size();
53+
}
54+
}
55+
```

0 commit comments

Comments
 (0)