File tree Expand file tree Collapse file tree
product-of-array-except-self
validate-binary-search-tree Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import java .util .*;
2+
3+ class Solution {
4+ public List <List <Integer >> threeSum (int [] nums ) {
5+ int n = nums .length ;
6+ Arrays .sort (nums );
7+
8+ Set <List <Integer >> result = new LinkedHashSet <>();
9+
10+ // N C 3 조합 전부 나열
11+ List <List <Integer >> possible = new ArrayList <>();
12+ for (int i = 0 ; i < n ; i ++) {
13+ for (int j = i + 1 ; j < n ; j ++) {
14+ for (int k = j + 1 ; k < n ; k ++) {
15+ possible .add (Arrays .asList (nums [i ], nums [j ], nums [k ]));
16+ }
17+ }
18+ }
19+
20+ // 합이 0인 것만 걸러서 Set에 넣기
21+ for (int i = 0 ; i < possible .size (); i ++) {
22+ List <Integer > array = possible .get (i );
23+ int sum = 0 ;
24+ for (int cur : array ) {
25+ sum += cur ;
26+ }
27+ if (sum == 0 ) {
28+ result .add (array ); // Set이라 중복은 알아서 걸러짐
29+ }
30+ }
31+
32+ // 3) 리턴할 때 List로 변환
33+ return new ArrayList <>(result );
34+ }
35+ }
Original file line number Diff line number Diff line change 1+ import java .util .*;
2+
3+ class Solution {
4+ // TC: O(2의 N승)
5+ // SC: O(N)
6+ public static Map <Integer , Integer > memo = new HashMap <>();
7+
8+ public int climbStairs (int n ) {
9+ // 1과 2로만 움직일 수 있을 때, 도달할 수 있는 모든 방법의 수에 대해 구하시오
10+ // DPS (QUEUE) , BPS (STACK)
11+ return dfs (n );
12+ }
13+
14+ // dfs(5) -> dfs(3) + dfs(4) -> dfs(2) + dfs(1) + dfs(3) + dfs(2)
15+ public static int dfs (int n ){
16+ if ( n == 1 ) return 1 ;
17+ if ( n == 2 ) return 2 ;
18+ if ( memo .containsKey (n ) ) return memo .get (n );
19+
20+ int result = dfs (n -1 ) + dfs (n -2 );
21+ memo .put (n , result );
22+ return result ;
23+ }
24+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int [] productExceptSelf (int [] nums ) {
3+ // 시간 복잡도가 O(N) 이어야 함.
4+ // 배열에서 나 자신을 빼고서 모두를 곱하라
5+ // 어떻게 해야 시간 복잡도가 O(N) 이지 ?
6+ int n = nums .length ;
7+ int zeroCount = 0 ;
8+ int maxTotal = 1 ; // 0이 아닌 값들의 곱
9+ for (int x : nums ) {
10+ if (x == 0 ) zeroCount ++;
11+ else maxTotal *= x ;
12+ }
13+
14+ int [] result = new int [n ];
15+ for (int i = 0 ; i < n ; i ++) {
16+ if (zeroCount >= 2 ) {
17+ result [i ] = 0 ; // 0이 2개 이상 → 무조건 0
18+ } else if (zeroCount == 1 ) {
19+ result [i ] = (nums [i ] == 0 ) ? maxTotal : 0 ; // 0 위치만 살아남음
20+ } else {
21+ result [i ] = maxTotal / nums [i ]; // 0 없음 → 그냥 나눔
22+ }
23+ }
24+ return result ;
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ import java .util .*;
2+
3+ class Solution {
4+ public boolean isAnagram (String s , String t ) {
5+ // 둘이 anagram 이면 인지 아닌지 확인 해라
6+ // 아나 그램이 다시 만들 수 있는 가 == 들어있는 알파벳의 갯수가 동일한 가
7+ Map <Character ,Integer > prevMap = new HashMap <>();
8+ Map <Character ,Integer > curMap = new HashMap <>();
9+
10+ for ( int i = 0 ; i < s .length (); i ++ ){
11+ prevMap .merge (s .charAt (i ), 1 , Integer ::sum );
12+ }
13+
14+ for ( int i = 0 ; i < t .length (); i ++){
15+ curMap .merge (t .charAt (i ),1 ,Integer ::sum );
16+ }
17+
18+ return prevMap .equals (curMap );
19+ }
20+ }
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+ class Solution {
17+ public boolean isValidBST (TreeNode root ) {
18+ return validate (root , Long .MIN_VALUE , Long .MAX_VALUE );
19+ }
20+
21+ private boolean validate (TreeNode node , long low , long high ) {
22+ if (node == null ) return true ;
23+ if (node .val <= low || node .val >= high ) return false ;
24+ return validate (node .left , low , node .val )
25+ && validate (node .right , node .val , high );
26+ }
27+ }
You can’t perform that action at this time.
0 commit comments