File tree Expand file tree Collapse file tree 3 files changed +115
-0
lines changed Expand file tree Collapse file tree 3 files changed +115
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .gatsby ;
2
+
3
+ /**
4
+ * @author -- gatsby
5
+ * @date -- 2022/7/12
6
+ * @description --
7
+ */
8
+
9
+
10
+ public class _208ImplementTrie {
11
+ class TrieNode {
12
+ boolean isEnd = false ;
13
+ // 直接创建一个26字母的数组,这样已经可以考虑到所有的下一位字母的情况
14
+ TrieNode [] children = new TrieNode [26 ];
15
+ }
16
+
17
+ private TrieNode root ;
18
+
19
+ public _208ImplementTrie () {
20
+ root = new TrieNode ();
21
+ }
22
+
23
+ public void insert (String word ) {
24
+ TrieNode p = root ;
25
+ for (int i = 0 ; i < word .length (); ++i ) {
26
+ int letter = word .charAt (i ) - 'a' ;
27
+ if (p .children [letter ] == null ) {
28
+ p .children [letter ] = new TrieNode ();
29
+ }
30
+ p = p .children [letter ];
31
+ }
32
+ // 此时在最后一个字母,将isEnd置为true
33
+ p .isEnd = true ;
34
+ }
35
+
36
+ public boolean search (String word ) {
37
+ TrieNode p = root ;
38
+ for (int i = 0 ; i < word .length (); ++i ) {
39
+ int letter = word .charAt (i ) - 'a' ;
40
+ if (p .children [letter ] == null ) return false ;
41
+ else p = p .children [letter ];
42
+ }
43
+ return p .isEnd ;
44
+ }
45
+
46
+ public boolean startsWith (String prefix ) {
47
+ TrieNode p = root ;
48
+ for (int i = 0 ; i < prefix .length (); ++i ) {
49
+ int letter = prefix .charAt (i ) - 'a' ;
50
+ if (p .children [letter ] == null ) return false ;
51
+ else p = p .children [letter ];
52
+ }
53
+ return true ;
54
+ }
55
+ }
Original file line number Diff line number Diff line change
1
+ package com .gatsby ;
2
+
3
+ /**
4
+ * @author -- gatsby
5
+ * @date -- 2022/4/30
6
+ * @description -- leetcode 908 最小差值
7
+ */
8
+
9
+
10
+ public class _908SmallestRangeI {
11
+ public int smallestRangeI (int [] nums , int k ) {
12
+ int max = Integer .MIN_VALUE ;
13
+ int min = Integer .MAX_VALUE ;
14
+ for (int num : nums ) {
15
+ if (num > max ) max = num ;
16
+ if (num < min ) min = num ;
17
+ }
18
+ return max - min > 2 * k ? max - min - 2 * k : 0 ;
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ package com .gatsby .offerII ;
2
+
3
+ import java .util .Arrays ;
4
+ import java .util .HashMap ;
5
+ import java .util .Map ;
6
+
7
+ /**
8
+ * @author -- gatsby
9
+ * @date -- 2022/7/13
10
+ * @description -- 剑指 Offer II 004. 只出现一次的数字
11
+ */
12
+
13
+
14
+ public class _4OnlyOnce {
15
+ public int singleNumber (int [] nums ) {
16
+ Map <Integer , Integer > count = new HashMap <>();
17
+ for (int num : nums ) {
18
+ count .put (num , count .getOrDefault (num , 0 ) + 1 );
19
+ }
20
+
21
+ for (int num : count .keySet ()) {
22
+ if (count .get (num ) == 1 ) return num ;
23
+ }
24
+
25
+ return -1 ;
26
+ }
27
+
28
+ public int single (int start , int [] nums ) {
29
+ return nums [start ] ^ nums [start + 1 ] ^ nums [start + 2 ];
30
+ }
31
+
32
+ public int singleNumberSort (int [] nums ) {
33
+ Arrays .sort (nums );
34
+
35
+ for (int i = 0 ; i + 3 < nums .length ; i += 3 ) {
36
+ if (nums [i ] != nums [i +2 ]) return single (i , nums );
37
+ }
38
+ return -1 ;
39
+ }
40
+ }
You can’t perform that action at this time.
0 commit comments