File tree Expand file tree Collapse file tree 5 files changed +130
-0
lines changed
product-of-array-except-self
validate-binary-search-tree Expand file tree Collapse file tree 5 files changed +130
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Time Complexity : O(n^2)
3
+ Space Complexity : O(1)
4
+ */
5
+ class Solution {
6
+ public List <List <Integer >> threeSum (int [] nums ) {
7
+ List <List <Integer >> result = new ArrayList <>();
8
+ Arrays .sort (nums );
9
+
10
+ for (int i = 0 ; i < nums .length - 2 ; i ++) {
11
+ if (i > 0 && nums [i ] == nums [i - 1 ]) continue ;
12
+ int low = i + 1 ;
13
+ int high = nums .length - 1 ;
14
+ while (low < high ) {
15
+ int threeSum = nums [i ] + nums [low ] + nums [high ];
16
+ if (threeSum < 0 ) {
17
+ low = low + 1 ;
18
+ } else if (threeSum > 0 ) {
19
+ high = high - 1 ;
20
+ } else {
21
+ result .add (Arrays .asList (nums [i ], nums [low ], nums [high ]));
22
+ while (low < high && nums [low ] == nums [low + 1 ]) low ++;
23
+ while (low < high && nums [high ] == nums [high - 1 ]) high --;
24
+ low = low + 1 ;
25
+ high = high - 1 ;
26
+ }
27
+ }
28
+ }
29
+
30
+ return result ;
31
+ }
32
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ Time Complexity : O(n)
3
+ Space Complexity : O(1)
4
+ */
5
+ class Solution {
6
+ public int climbStairs (int n ) {
7
+ if (n < 3 ) return n ;
8
+ int prev = 1 ;
9
+ int curr = 2 ;
10
+
11
+ for (int i = 0 ; i < n - 2 ; i ++) {
12
+ int temp = prev ;
13
+ prev = curr ;
14
+ curr = temp + curr ;
15
+ }
16
+ return curr ;
17
+ }
18
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ Time Complexity : O(n)
3
+ Space Complexity : O(n)
4
+ */
5
+ class Solution {
6
+ public int [] productExceptSelf (int [] nums ) {
7
+ int [] results = new int [nums .length ];
8
+ Arrays .fill (results , 1 );
9
+ int before = 1 ;
10
+ int after = 1 ;
11
+
12
+ for (int i = 0 ; i < nums .length - 1 ; i ++) {
13
+ before = before * nums [i ];
14
+ results [i + 1 ] = results [i + 1 ] * before ;
15
+ }
16
+
17
+ for (int i = nums .length - 1 ; i > 0 ; i --) {
18
+ after = after * nums [i ];
19
+ results [i - 1 ] = results [i - 1 ] * after ;
20
+ }
21
+ return results ;
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ - 애너그램 또는 어구정철 : 문장의 순서를 바꾸어 다른 단어로 만드는 것
3
+ - 같은 단어가 맞는지 검증
4
+ - s와 t의 각 알파벳별로 한쪽은 증가, 한쪽은 감소하면서 갯수 세기
5
+ - 갯수가 0이 아닌 경우엔 서로 다른 단어이기에 false
6
+ */
7
+ /*
8
+ Time Complexity : O(n)
9
+ Space Complexity : O(1)
10
+ */
11
+ class Solution {
12
+ public boolean isAnagram (String s , String t ) {
13
+ if (s .length () != t .length ())
14
+ return false ;
15
+
16
+ int [] count = new int [26 ];
17
+ for (int i = 0 ; i < s .length (); i ++) {
18
+ count [s .charAt (i ) - 'a' ]++;
19
+ count [t .charAt (i ) - 'a' ]--;
20
+ }
21
+
22
+ for (int c : count ) {
23
+ if (c != 0 )
24
+ return false ;
25
+ }
26
+ return true ;
27
+ }
28
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * public class TreeNode {
4
+ * int val;
5
+ * TreeNode left;
6
+ * TreeNode right;
7
+ * TreeNode() {}
8
+ * TreeNode(int val) { this.val = val; }
9
+ * TreeNode(int val, TreeNode left, TreeNode right) {
10
+ * this.val = val;
11
+ * this.left = left;
12
+ * this.right = right;
13
+ * }
14
+ * }
15
+ */
16
+ /*
17
+ Time Complexity : O(n)
18
+ Space Complexity : O(n)
19
+ */
20
+ class Solution {
21
+ TreeNode prev ;
22
+ public boolean isValidBST (TreeNode root ) {
23
+ if (root == null ) return true ;
24
+ if (!isValidBST (root .left )) return false ;
25
+ if (prev != null && prev .val >= root .val ) return false ;
26
+ prev = root ;
27
+ return isValidBST (root .right );
28
+ }
29
+ }
You can’t perform that action at this time.
0 commit comments