Skip to content

Commit f1a6b95

Browse files
committed
[Function add]
1. Add conclusion about treeset. 2. Add leetcode solution.
1 parent d0a20b8 commit f1a6b95

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

DataStructrue/Set/TreeSet.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Data structure | TreeSet
2+
3+
## Introduction
4+
Treeset is a navigable set which also guaranteed the unique of the elements in the tree set. The implementation of Treeset is based on TreeMap and we can inject a comparator to the tree set so we can have have the elements in the set saved in order.
5+
6+
![Imgur](https://i.imgur.com/CHIb1Hz.png)
7+
8+
## Constructor of TreeSet
9+
TreeSet is implemtented based on TreeMap so it may require two kinds of elements which are Comparator and Collection. Comparator is used for navigable set and Collection is the container for save elements.
10+
```Java
11+
TreeSet(Collection<? extends E> collection);
12+
TreeSet(Comparator<? super E> comparator);
13+
```
14+
15+
## APIs of TreeSet
16+
1. TreeSet, as well as a set, has the basic ability of CRUD of set data structure. So the API of the TreeSet CRUD is the same as HashSet:
17+
```Java
18+
// Add element(s) into TreeSet.
19+
public boolean add(E e);
20+
public boolean addAll(Collection<? extends E> c);
21+
// Delete element in the TreeSet.
22+
public boolean remove(Object o);
23+
// Size of the treeset
24+
public int size();
25+
// Check if current TreeSet is empty.
26+
public boolean isEmpty();
27+
```
28+
29+
2. TreeSet also implements the Navigable interface, so we can use some of the APIs clarified in this interface. The main idea of this interface is to have the elements saved in this data structure in default(or customized) order.
30+
```Java
31+
// Return a iterator of current treeset and this iterator will return elements in ascending order.
32+
public Iterator<E> iterator();
33+
// Return a iterator of current treeset and this iterator will return elements in descending order.
34+
public Iterator<E> descendingIterator();
35+
// TreeSet also worked as a queue, so we have the API of dequeue the first element in the set.
36+
public E pollFirst();
37+
public E pollLast();
38+
```
39+
40+
## Attention
41+
1. TreeSet is not thread safe.
42+
43+
## Reference
44+
1. [Java 集合系列17之 TreeSet详细介绍(源码解析)和使用示例](https://www.cnblogs.com/skywang12345/p/3311268.html)

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,8 @@
383383

384384
[219. Contains Duplicate II](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/219.%20Contains%20Duplicate%20II.md)
385385

386+
[220. Contains Duplicate III](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/220.%20Contains%20Duplicate%20III.md)
387+
386388
## Algorithm(4th_Edition)
387389
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.
388390
All java realization codes are placed in different packages.

leetcode/220. Contains Duplicate III.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,37 @@ class Solution {
6666
return false;
6767
}
6868
}
69+
```
70+
71+
### 二刷
72+
1. 使用for循环, O(N^2).
73+
```Java
74+
class Solution {
75+
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
76+
for(int i = 0; i < nums.length; i++){
77+
for(int j = i + 1; j < nums.length && j - i <= k; j++){
78+
if(Math.abs((long)nums[j] - (long)nums[i]) <= t) return true;
79+
}
80+
}
81+
return false;
82+
}
83+
}
84+
```
85+
86+
2. 使用TreeSet来维护K个元素在集合中,我们通过遍历整个数组来判断nums[i] - t, nums[i] + t的子集是不是为空,如果不为空则返回true,不然我们将当前的元素加入集合并移除最开始加入的元素(保证集合中元素的个数)。
87+
```Java
88+
class Solution {
89+
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
90+
if(k <= 0 || t < 0) return false;
91+
int len = nums.length;
92+
TreeSet<Long> set = new TreeSet<>();
93+
for(int i = 0; i < len; i++){
94+
Set<Long> subSet = set.subSet((long)nums[i] - t, true, (long)nums[i] + t, true);
95+
if(!subSet.isEmpty()) return true;
96+
if(i - k >= 0) set.remove((long)nums[i - k]);
97+
set.add((long)nums[i]);
98+
}
99+
return false;
100+
}
101+
}
69102
```

0 commit comments

Comments
 (0)