File tree Expand file tree Collapse file tree 5 files changed +61
-1
lines changed
best-time-to-buy-and-sell-stock Expand file tree Collapse file tree 5 files changed +61
-1
lines changed Original file line number Diff line number Diff line change 11"""
22https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
33"""
4+
5+
6+ class Solution :
7+ def maxProfit (self , prices : List [int ]) -> int :
8+ max_profit = 0
9+ min_price = prices [0 ]
10+
11+ for price in prices :
12+ profit = price - min_price
13+ min_price = min (min_price , price )
14+ max_profit = max (profit , max_profit )
15+
16+ return max_profit
Original file line number Diff line number Diff line change 11"""
22https://leetcode.com/problems/contains-duplicate/
33"""
4+
5+
6+ class Solution :
7+ def containsDuplicate (self , nums : List [int ]) -> bool :
8+ my_dict = {}
9+
10+ for num in nums :
11+ if num in my_dict :
12+ return True
13+ my_dict [num ] = 0
14+
15+ return False
Original file line number Diff line number Diff line change 11"""
22https://leetcode.com/problems/two-sum/
33"""
4- #
54
5+
6+ class Solution :
7+ def twoSum (self , nums : List [int ], target : int ) -> List [int ]:
8+ numMap = {}
9+
10+ for i in range (len (nums )):
11+ complement = target - nums [i ]
12+ if complement in numMap :
13+ return [numMap [complement ], i ]
14+
15+ numMap [nums [i ]] = i
Original file line number Diff line number Diff line change 11"""
22https://leetcode.com/problems/valid-anagram/
33"""
4+
5+
6+ class Solution :
7+ def isAnagram (self , s : str , t : str ) -> bool :
8+ my_dict1 = {}
9+ my_dict2 = {}
10+
11+ for char in s :
12+ my_dict1 [char ] = my_dict1 .get (char , 0 ) + 1
13+
14+ for char in t :
15+ my_dict2 [char ] = my_dict2 .get (char , 0 ) + 1
16+
17+ return my_dict1 == my_dict2
Original file line number Diff line number Diff line change 11"""
22https://leetcode.com/problems/valid-palindrome/
33"""
4+
5+
6+ class Solution :
7+ def isPalindrome (self , s : str ) -> bool :
8+ alphanumeric_chars = []
9+ for char in s :
10+ if char .isalnum ():
11+ lowercase_letter = char .lower ()
12+ alphanumeric_chars .append (lowercase_letter )
13+
14+ return alphanumeric_chars == alphanumeric_chars [::- 1 ]
You can’t perform that action at this time.
0 commit comments