Skip to content

Commit

Permalink
6 days
Browse files Browse the repository at this point in the history
  • Loading branch information
conordewey3 committed Oct 20, 2018
1 parent 451c6e0 commit 0345af2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
8 changes: 4 additions & 4 deletions leetcode/longest_palindromic_substring.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
'''

# Brute force approach - O(n) time, O(n) space
# Brute force approach - O(n^3) time, O(1) space
def longestPalindrome(self, s):
longest_str = ''
for i in range(len(s)):
for j in range(i, len(s) + 1):
temp = s[s:j]
if temp == temp[::-1] and len(temp) > len(longest_str):
temp = s[i:j]
if temp == temp[::-1] and len(temp) >= len(longest_str):
longest_str = temp
print(temp)
return longest_str
32 changes: 32 additions & 0 deletions leetcode/single_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

'''
136. Single Number
Given a non-empty array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,1]
Output: 1
Example 2:
Input: [4,1,2,1,2]
Output: 4
'''

# Dictionary approach - O(n) time, O(n) space
def singleNumber(self, nums):
d = {}
for item in nums:
if item in d:
d[item] += 1
else:
d[item] = 1

for key, value in d.items():
if value == 1:
return key

0 comments on commit 0345af2

Please sign in to comment.