Skip to content

Commit c9ffdc8

Browse files
committed
Solve leetcode 1, 217, 409, and 88
1 parent 74bd35e commit c9ffdc8

4 files changed

+95
-0
lines changed

1-Two Sum.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Link: https://leetcode.com/problems/two-sum/
2+
3+
def twoSum(nums, target):
4+
# key: Number that adds up to target
5+
# value: Index the number is at
6+
two_sum = {}
7+
8+
# Iterate through nums using indices b/c we need the indices
9+
# for the final ans
10+
for i in range(len(nums)):
11+
curr_num = nums[i]
12+
# Case 1: We found a pair that adds up to target
13+
if curr_num in two_sum:
14+
return [two_sum[curr_num], i]
15+
# Case 2: Add a new entry for the current number
16+
else:
17+
two_sum[target-curr_num] = i
18+
19+
# If we go through the entire for loop without returning
20+
# there's no valid answer
21+
return -1

217-Contains Duplicate.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Link: https://leetcode.com/problems/contains-duplicate/
2+
3+
def containsDuplicate(nums):
4+
# A set keeping track of the numbers encountered so far
5+
num_seen = set()
6+
7+
for curr_num in nums:
8+
# Case 1: We have seen curr_num earlier on in nums
9+
if curr_num in num_seen:
10+
return True
11+
# Case 2: First encounter with curr_num
12+
else:
13+
num_seen.add(curr_num)
14+
15+
# If we go through the entire for loop without returning
16+
# there are no duplicates
17+
return False

409-Longest Palindrome.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Link: https://leetcode.com/problems/longest-palindrome/
2+
3+
def longestPalindrome(s):
4+
# key: unique character
5+
# value: number of times said character occurs in s
6+
char_count = {}
7+
8+
# Populate the char_count dictionary
9+
for char in s:
10+
if char in char_count:
11+
char_count[char] += 1
12+
else:
13+
char_count[char] = 1
14+
15+
palindrome = 0 # Length of the longest palindrome
16+
odd = False # Whether there's at least one odd num occ char
17+
18+
for count in char_count.values():
19+
# Case 1: The current character occurs an odd number of times
20+
if count%2 != 0:
21+
odd = True # Set odd flag
22+
# Add an even number of those characters
23+
palindrome += (count-1)
24+
# Case 2: The current character occurs an even number of times
25+
else:
26+
palindrome += count
27+
28+
# If we encountered at least one oddly occuring character
29+
if odd:
30+
# Add 1 since you can have an odd number of chars in the middle
31+
# of the palindrome
32+
palindrome += 1
33+
34+
return palindrome

88-Merge Sorted Array.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# https://leetcode.com/problems/merge-sorted-array/
2+
3+
def merge(nums1, m, nums2, n):
4+
# Iterate through both of the sorted arrays at the same time
5+
while m > 0 and n > 0:
6+
curr_1 = nums1[m-1]
7+
curr_2 = nums2[n-1]
8+
9+
if curr_1 >= curr_2:
10+
nums1[(m+n)-1] = curr_1
11+
m -= 1
12+
else: # curr_2 > curr_1
13+
nums1[(m+n)-1] = curr_2
14+
n -= 1
15+
16+
# If there are smaller elements left in nums2, copy them over.
17+
# No need to do this for leftover num1 elements, since we're
18+
# merging into nums1 and the elements will already be there.
19+
while n > 0:
20+
nums1[n-1] = nums2[n-1]
21+
n -= 1
22+
23+
return nums1

0 commit comments

Comments
 (0)