File tree Expand file tree Collapse file tree 5 files changed +140
-0
lines changed
kth-smallest-element-in-a-bst Expand file tree Collapse file tree 5 files changed +140
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public boolean containsDuplicate (int [] nums ) {
3
+ // HashSet O(n)
4
+ /*
5
+ Set<Integer> set = new HashSet();
6
+ for (int num : nums) set.add(num);
7
+ return set.size() != nums.length;
8
+ */
9
+
10
+ // dupl value O(n log n)
11
+ Arrays .sort (nums );
12
+ for (int i = 0 ; i < nums .length - 1 ; i ++) {
13
+ if (nums [i ] == nums [i + 1 ]) return true ;
14
+ }
15
+ return false ;
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ private List <Integer > nums = new ArrayList <>();
3
+ public int kthSmallest (TreeNode root , int k ) {
4
+ visitTreeNode (root );
5
+ return nums .get (k -1 );
6
+ }
7
+
8
+ public void visitTreeNode (TreeNode node ) {
9
+ if (node == null ) return ;
10
+
11
+ // left < right
12
+ visitTreeNode (node .left );
13
+ nums .add (node .val );
14
+ visitTreeNode (node .right );
15
+ }
16
+ // time complexity: O(n), visit all nodes once
17
+ // space complexity: O(1), used an array list
18
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int hammingWeight (int n ) {
3
+ /*
4
+ Time complexity: O(n)
5
+ Space complexity: O(1)
6
+ */
7
+ return Integer .bitCount (n );
8
+
9
+ /*
10
+ Time complexity: O(n)
11
+ Space complexity: O(1)
12
+
13
+ int output = 0;
14
+ while (n > 0) {
15
+ if ((n & 1) == 1) output += 1;
16
+ n >>= 1;
17
+ }
18
+ return output;
19
+ */
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int countSubstrings (String s ) {
3
+ /**
4
+ * time complexity: O(n);
5
+ * space complexity: O(1);
6
+ */
7
+
8
+ int output = 0 ;
9
+ // ex) abc
10
+ // 1. abc
11
+ // 2. bc
12
+ // 3. c
13
+ for (int i = 0 ; i < s .length (); i ++) {
14
+ output += palindromicCheck (s .substring (i , s .length ()));
15
+ }
16
+ return output ;
17
+ }
18
+
19
+ public int palindromicCheck (String str ) {
20
+ int result = 0 ;
21
+ /**
22
+ * ex) abc
23
+ * 1. a
24
+ * 2. ab
25
+ * 3. abc
26
+ */
27
+ for (int i = 0 ; i < str .length (); i ++) {
28
+ String candidate = str .substring (0 , i +1 );
29
+ if (palindromic (candidate )) {
30
+ result += 1 ;
31
+ }
32
+ }
33
+ return result ;
34
+ }
35
+
36
+ public boolean palindromic (String candidate ) {
37
+ int start = 0 ;
38
+ int end = candidate .length () - 1 ;
39
+
40
+ /** ex)abc
41
+ * 1. a -> true
42
+ * 2. ab -> false
43
+ * 3. abc -> false
44
+ */
45
+ while (start < end ) {
46
+ if (candidate .charAt (start ) != candidate .charAt (end )) return false ;
47
+ start += 1 ;
48
+ end -= 1 ;
49
+ }
50
+ return true ;
51
+ }
52
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int [] topKFrequent (int [] nums , int k ) {
3
+
4
+ // declare hashmap
5
+ // key: each element, value: appered count
6
+ Map <Integer , Integer > map = new HashMap <>();
7
+
8
+ // if map contains the element, increase its value by one.
9
+ // else put the element and 1 for initializing
10
+ for (int num : nums ) {
11
+ if (map .containsKey (num )) {
12
+ map .put (num , map .getOrDefault (num , 0 ) + 1 );
13
+ } else {
14
+ map .put (num , 1 );
15
+ }
16
+ }
17
+
18
+ // keyList only has key values of the hashmap
19
+ // using their value count sort keys by descending order
20
+ List <Integer > keyList = new ArrayList <>(map .keySet ());
21
+ Collections .sort (keyList , (o1 , o2 ) -> map .get (o2 ).compareTo (map .get (o1 )));
22
+
23
+
24
+ int [] output = new int [k ];
25
+ int idx = 0 ;
26
+
27
+ // retreive keys k times and set output
28
+ while (idx < k ) output [idx ] = keyList .get (idx ++);
29
+
30
+ return output ;
31
+ }
32
+ }
You can’t perform that action at this time.
0 commit comments