File tree Expand file tree Collapse file tree 5 files changed +106
-0
lines changed
product-of-array-except-self
validate-binary-search-tree Expand file tree Collapse file tree 5 files changed +106
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ 시간 복잡도: O(N^2)
3+ 공간 복잡도: O(N)
4+ """
5+ class Solution :
6+ def threeSum (self , nums : List [int ]) -> List [List [int ]]:
7+ nums .sort ()
8+
9+ result = set ()
10+
11+ for i in range (len (nums ) - 2 ):
12+ start , end = i + 1 , len (nums ) - 1
13+ while start < end :
14+ temp = nums [i ] + nums [start ] + nums [end ]
15+ if temp == 0 :
16+ result .add ((nums [i ], nums [start ], nums [end ]))
17+ start += 1
18+ end -= 1
19+ elif temp < 0 :
20+ start += 1
21+ else :
22+ end -= 1
23+
24+ return list (result )
Original file line number Diff line number Diff line change 1+ """
2+ 시간 복잡도: O(N)
3+ 공간 복잡도: O(N)
4+ """
5+ class Solution :
6+ def climbStairs (self , n : int ) -> int :
7+ dp = [0 ] * 46
8+ dp [1 ], dp [2 ] = 1 , 2
9+
10+ for i in range (3 , n + 1 ):
11+ dp [i ] = dp [i - 1 ] + dp [i - 2 ]
12+
13+ return dp [n ]
Original file line number Diff line number Diff line change 1+ """
2+ 시간 복잡도: O(N)
3+ 공간 복잡도: O(N)
4+ """
5+ class Solution :
6+ def productExceptSelf (self , nums : List [int ]) -> List [int ]:
7+ prefix = [1 ] * len (nums )
8+ for i in range (len (nums ) - 1 ):
9+ prefix [i + 1 ] = prefix [i ] * nums [i ]
10+
11+ suffix = [1 ] * len (nums )
12+ for i in range (len (nums ) - 1 , 0 , - 1 ):
13+ suffix [i - 1 ] = suffix [i ] * nums [i ]
14+
15+ result = []
16+ for i , j in zip (prefix , suffix ):
17+ result .append (i * j )
18+
19+ return result
Original file line number Diff line number Diff line change 1+ """
2+ 시간 복잡도: O(N)
3+ 공간 복잡도: O(N)
4+
5+ 코드 가독성 개선 코드:
6+ from collections import Counter
7+
8+ class Solution:
9+ def isAnagram(self, s: str, t: str) -> bool:
10+ return Counter(s) == Counter(t)
11+ """
12+ from collections import Counter
13+
14+ class Solution :
15+ def isAnagram (self , s : str , t : str ) -> bool :
16+ if len (s ) != len (t ):
17+ return False
18+
19+ s_counter = Counter (s )
20+ t_counter = Counter (t )
21+
22+ for num , freq in t_counter .items ():
23+ if s_counter [num ] != freq :
24+ return False
25+
26+ return True
Original file line number Diff line number Diff line change 1+ """
2+ 시간 복잡도: O(N)
3+ 공간 복잡도: O(N)
4+ """
5+ class Solution :
6+ def isValidBST (self , root : Optional [TreeNode ]) -> bool :
7+ num_list = []
8+
9+ def dfs (node : TreeNode ):
10+ if not node :
11+ return
12+
13+ dfs (node .left )
14+ num_list .append (node .val )
15+ dfs (node .right )
16+
17+ dfs (root )
18+
19+ mem = num_list [0 ]
20+ for i in range (1 , len (num_list )):
21+ if mem >= num_list [i ]:
22+ return False
23+ mem = num_list [i ]
24+ return True
You can’t perform that action at this time.
0 commit comments