This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def productExceptSelf(self, nums: List[int]) -> List[int]: | |
| current = 1 | |
| res = [1] * len(nums) | |
| for index, num in enumerate(nums): | |
| res[index] *= current | |
| current *= num | |
| current = 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def isValid(self, s: str) -> bool: | |
| brackets = {'}':'{', ')': '(', ']': '['} | |
| stack = [] | |
| for char in s: | |
| if char in brackets: | |
| current = stack.pop() if stack else '' | |
| if brackets[char] != current: | |
| return False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from collections import defaultdict | |
| def isAnagram(self, s: str, t: str) -> bool: | |
| results = defaultdict(int) | |
| for char in s: | |
| results[char] += 1 | |
| for char in t: | |
| results[char] -= 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from collections import Counter | |
| def isAnagram(self, s: str, t: str) -> bool: | |
| return Counter(s) == Counter(t) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def isAnagram(self, s: str, t: str) -> bool: | |
| letters1 = [0] * 26 | |
| letters2 = [0] * 26 | |
| for char in s: | |
| letters1[ord(char) - ord('a')] += 1 | |
| for char in t: | |
| letters2[ord(char) - ord('a')] += 1 | |
| for i in range(26): | |
| if letters1[i] != letters2[i]: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def isAnagram(self, s: str, t: str) -> bool: | |
| return sorted(s) == sorted(t) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def maxProfit(self, prices: List[int]) -> int: | |
| max_profit = 0 | |
| for i in range(len(prices)): | |
| for j in range(i+1, len(prices)): | |
| profit = prices[j] - prices[i] | |
| max_profit = max(max_profit, profit) | |
| return max_profit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def maxProfit(self, prices: List[int]) -> int: | |
| min_price = float('inf') | |
| max_profit = 0 | |
| for price in prices: | |
| min_price = min(min_price, price) | |
| max_profit = max(max_profit, price - min_price) | |
| return max_profit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def containsDuplicate(self, nums: List[int]) -> bool: | |
| seen = set() | |
| for num in nums: | |
| if num in seen: | |
| return True | |
| seen.add(num) | |
| return False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def containsDuplicate(self, nums: List[int]) -> bool: | |
| nums.sort() | |
| for i in range(1, len(nums)): | |
| if nums[i] == nums[i-1]: | |
| return True | |
| return False |
NewerOlder