File tree Expand file tree Collapse file tree 3 files changed +96
-0
lines changed Expand file tree Collapse file tree 3 files changed +96
-0
lines changed Original file line number Diff line number Diff line change 1+ # Definition for singly-linked list.
2+ # class ListNode:
3+ # def __init__(self, x):
4+ # self.val = x
5+ # self.next = None
6+
7+ """
8+ TC: O(n)
9+ SC: O(n)
10+ """
11+ class Solution :
12+ def hasCycle (self , head : Optional [ListNode ]) -> bool :
13+ visited = set ()
14+ while head :
15+ # ํด๋น ๋
ธ๋ ์ด๋ฏธ ๋ฐฉ๋ฌธํ ์ ์์ผ๋ฉด
16+ if head in visited :
17+ return True
18+ # ๋ฐฉ๋ฌธํ ์ ์์ผ๋ฉด set์ ๋ฃ๊ธฐ
19+ visited .add (head )
20+ # ๋ค์ ๋
ธ๋๋ก ์ด๋
21+ head = head .next
22+
23+ # cycle์ด ์์
24+ return False
25+
26+ """
27+ The tortoise and hare
28+
29+ TC: O(n)
30+ SC: O(1)
31+ """
32+ class Solution :
33+ def hasCycle (self , head : Optional [ListNode ]) -> bool :
34+ slow , fast = head , head
35+ while fast and fast .next :
36+ slow = slow .next
37+ fast = fast .next .next
38+ if slow == fast :
39+ return True
40+ return False
Original file line number Diff line number Diff line change 1+ """
2+ TC: O(n)
3+ SC: O(1)
4+ """
5+
6+ class Solution :
7+ def maxProduct (self , nums : List [int ]) -> int :
8+ max_here = min_here = ans = nums [0 ]
9+
10+ for x in nums [1 :]:
11+ if x < 0 :
12+ max_here , min_here = min_here , max_here
13+ max_here = max (x , max_here * x )
14+ min_here = min (x , min_here * x )
15+ ans = max (ans , max_here )
16+ return ans
Original file line number Diff line number Diff line change 1+ /**
2+ * JavaScript ๋นํธ ์ฐ์ฐ์ ์ ๋ฆฌ:
3+ *
4+ * 1. & (AND ์ฐ์ฐ์): ๋ ๋นํธ๊ฐ ๋ชจ๋ 1์ผ ๋๋ง 1์ ๋ฐํ
5+ * ์: 5 & 3 = 1 (101 & 011 = 001)
6+ *
7+ * 2. ^ (XOR ์ฐ์ฐ์): ๋ ๋นํธ๊ฐ ๋ค๋ฅผ ๋ 1์ ๋ฐํ
8+ * ์: 5 ^ 3 = 6 (101 ^ 011 = 110)
9+ *
10+ * 3. << (์ผ์ชฝ ์ํํธ): ๋นํธ๋ฅผ ์ผ์ชฝ์ผ๋ก ์ด๋ (๊ณฑํ๊ธฐ 2์ ๋์ผ)
11+ * ์: 5 << 1 = 10 (101 << 1 = 1010)
12+ *
13+ * ํ์ด ์ค๋ช
:
14+ * - ์ด ์๊ณ ๋ฆฌ์ฆ์ ๋ง์
์ ๋นํธ ์ฐ์ฐ์ผ๋ก ๊ตฌํ
15+ * - XOR(^)๋ก ์๋ฆฌ์ฌ๋ฆผ ์๋ ํฉ์ ๊ตฌํจ
16+ * - AND(&)๋ก ์๋ฆฌ์ฌ๋ฆผ์ ์ฐพ๊ณ , ์ผ์ชฝ ์ํํธ(<<)๋ก ํ ์๋ฆฌ ์ฌ๋ฆผ
17+ * - ์๋ฆฌ์ฌ๋ฆผ์ด 0์ด ๋ ๋๊น์ง ๋ฐ๋ณต
18+ *
19+ * ์๊ณต๊ฐ ๋ณต์ก๋:
20+ * - ์๊ฐ ๋ณต์ก๋: O(1) - ์ต์
์ ๊ฒฝ์ฐ 32๋ฒ ๋ฐ๋ณต (32๋นํธ ์ ์)
21+ * - ๊ณต๊ฐ ๋ณต์ก๋: O(1) - ์์ ๊ณต๊ฐ๋ง ์ฌ์ฉ
22+ */
23+
24+ /**
25+ * @param {number } a
26+ * @param {number } b
27+ * @return {number }
28+ */
29+ var getSum = function ( a , b ) {
30+ while ( b !== 0 ) {
31+ // ์๋ฆฌ์ฌ๋ฆผ
32+ let carry = ( a & b ) << 1 ;
33+
34+ // ์๋ฆฌ์ฌ๋ฆผ ์๋ ํฉ
35+ a = a ^ b ;
36+
37+ b = carry ;
38+ }
39+ return a ;
40+ } ;
You canโt perform that action at this time.
0 commit comments