Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## Problem1 (https://leetcode.com/problems/subarray-sum-equals-k/)

class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
r_sum = 0
hm = {
0:1
}
count = 0
for num in nums:
r_sum += num
comp = r_sum - k
if comp in hm:
count+=hm[comp]
if r_sum in hm:
hm[r_sum]+=1
else:
hm[r_sum]=1
return count
20 changes: 20 additions & 0 deletions problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Problem2 (https://leetcode.com/problems/contiguous-array/)

class Solution:
def findMaxLength(self, nums: List[int]) -> int:
r_sum = 0
max_length = 0
my_map = {
0:-1
}
for i in range(len(nums)):
if nums[i] == 0:
r_sum-=1
else:
r_sum+=1

if r_sum in my_map:
max_length = max(max_length,i-my_map[r_sum])
else:
my_map[r_sum] = i
return max_length
15 changes: 15 additions & 0 deletions problem3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Problem3 (https://leetcode.com/problems/longest-palindrome)

class Solution:
def longestPalindrome(self, s: str) -> int:
my_set = set()
count = 0
for i in s:
if i in my_set:
count+=2
my_set.remove(i)
else:
my_set.add(i)
if len(my_set) > 0:
return count+1
return count