File tree Expand file tree Collapse file tree 5 files changed +64
-0
lines changed
best-time-to-buy-and-sell-stock Expand file tree Collapse file tree 5 files changed +64
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
3+ """
4+
5+ class Solution :
6+ def maxProfit (self , prices : List [int ]) -> int :
7+ now = prices [0 ]
8+ diff = [0 ] * len (prices )
9+
10+ for i , v in enumerate (prices [1 :]):
11+ if now > v :
12+ now = v
13+ else :
14+ diff [i + 1 ] = v - now
15+
16+ return max (diff )
Original file line number Diff line number Diff line change 1+ """
2+ https://leetcode.com/problems/contains-duplicate/
3+ """
4+
5+ class Solution :
6+ def containsDuplicate (self , nums : List [int ]) -> bool :
7+ visited = {}
8+ for n in nums :
9+ if n in visited :
10+ return True
11+ visited [n ] = True
12+ return False
Original file line number Diff line number Diff line change 1+ """
2+ https://leetcode.com/problems/two-sum/
3+ """
4+
5+ class Solution :
6+ def twoSum (self , nums : List [int ], target : int ) -> List [int ]:
7+ s , e = 0 , len (nums ) - 1
8+ arr = [[n , i ] for i , n in enumerate (nums )]
9+
10+ arr .sort (key = lambda x : x [0 ])
11+
12+ while s <= e :
13+ now = arr [s ][0 ] + arr [e ][0 ]
14+ if now == target :
15+ return [arr [s ][1 ], arr [e ][1 ]]
16+ if now < target :
17+ s += 1
18+ else :
19+ e -= 1
Original file line number Diff line number Diff line change 1+ """
2+ https://leetcode.com/problems/valid-anagram/
3+ """
4+
5+ from collections import Counter
6+
7+ class Solution :
8+ def isAnagram (self , s : str , t : str ) -> bool :
9+ return Counter (s ) == Counter (t )
Original file line number Diff line number Diff line change 1+ """
2+ https://leetcode.com/problems/valid-palindrome/
3+ """
4+
5+ class Solution :
6+ def isPalindrome (self , s : str ) -> bool :
7+ s = "" .join ([i for i in s if i .isalnum ()]).lower ()
8+ return s == s [::- 1 ]
You can’t perform that action at this time.
0 commit comments