Skip to content

Commit

Permalink
Merge pull request #4 from Nenu1985/leetcode
Browse files Browse the repository at this point in the history
Leetcode
  • Loading branch information
Nenu1985 authored Aug 6, 2021
2 parents 354dc43 + 8ce91fd commit a558e17
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 0 deletions.
54 changes: 54 additions & 0 deletions leetcode/add two numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

from typing import List

# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next

class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""

carry = 0
head = first_head = ListNode()

while l1 or l2 or carry:
result = 0
if l1:
result += l1.val
l1 = l1.next
if l2:
result += l2.val
l2 = l2.next
if carry:
result += carry

carry = result // 10
val = result % 10
head.next = ListNode(val)
head = head.next
return first_head.next

def stringToListNode(self,number_string: List[int]):
previousNode = None
first = None
for i in number_string:
currentNode = ListNode(i)
if first is None:
first = currentNode
if previousNode is not None:
previousNode.next = currentNode
previousNode = currentNode
return first

if __name__ == '__main__':
s = Solution()
l1 = s.stringToListNode([2,4,3])
l2 = s.stringToListNode([5,6,4])
s.addTwoNumbers(l1, l2)
69 changes: 69 additions & 0 deletions leetcode/is_palindrome.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from typing import Tuple, List


class Solution:
def longestPalindrome(self, s: str) -> str:
# max_result_length = 0
# max_result_idx = 0
# d = {}
# times = 0
# for idx in range(len(s)):
# if len(s) - idx < max_result_length:
# break
# for count_letters in range(1, len(s) + 1):
# if count_letters < max_result_length:
# continue
# substring_to_check = s[idx:count_letters]
# if substring_to_check not in d:
# is_palindrom, n = self.is_palindrom(substring_to_check)
# times += 1
# if is_palindrom:
# if n > max_result_length:
# max_result_length = n
# max_result_idx = idx
# d[substring_to_check] = (True, idx, n)
# else:
# d[substring_to_check] = (False, idx, n)

# # is_palindrom, n = self.is_palindrom(substring_to_check)
# # times += 1
# # if is_palindrom:
# # if n > max_result_length:
# # max_result_length = n
# # max_result_idx = idx
# # d[substring_to_check] = (True, n)
# output = s[max_result_idx: max_result_length + max_result_idx]
# return output
dp: List[List[bool]] = [[False] * len(s) for _ in range(len(s))]
length: int = 0
result: str = ''
times = 0
temp_results = []
for end in range(len(s)):
for start in range(end+1):
if start == end:
dp[start][end] = True
elif start + 1 == end:
dp[start][end] = s[start] == s[end]
else:
dp[start][end] = s[start] == s[end] and dp[start+1][end-1]

if dp[start][end] and end-start+1 > length:
times += 1
length = end-start+1
result = s[start:end+1]
temp_results.append(result)

return result
def is_palindrom(self, s: str) -> Tuple[bool, int]:
if s == s[::-1]:
return True, len(s)
else:
return False, 0

if __name__ == '__main__':
s = Solution()
# print(s.longestPalindrome("cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"))
# print(s.longestPalindrome('forgeeksskeegfor'))
# print(s.longestPalindrome('a'))
print(s.longestPalindrome('twbiqwtafgjbtolwprpdnymaatlbuacrmzzwhkpvuwdtyfjsbsqxrlxxtqkjlpkvpxmlajdmnyepsmczmmfdtjfbyybotpoebilayqzvqztqgddpcgpelwmriwmoeeilpetbxoyktizwcqeeivzgxacuotnlzutdowiudwuqnghjgoeyojikjhlmcsrctvnahnoapmkcrqmwixpbtirkasbyajenknuccojooxfwdeflmxoueasvuovcayisflogtpxtbvcxfmydjupwihnxlpuxxcclbhvutvvshcaikuedhyuksbwwjsnssizoedjkbybwndxpkwcdxaexagazztuiuxphxcedqstahmevkwlayktrubjypzpaiwexkwbxcrqhkpqevhxbyipkmlqmmmogrnexsztsbkvebjgybrolttvnidnntpgvsawgaobycfaaviljsvyuaanguhohsepbthgjyqkicyaxkytshqmtxhilcjxdpcbmvnpippdrpggyohwyswuydyrhczlxyyzregpvxyfwpzvmjuukswcgpenygmnfwdlryobeginxwqjhxtmbpnccwdaylhvtkgjpeyydkxcqarkwvrmwbxeetmhyoudfuuwxcviabkqyhrvxbjmqcqgjjepmalyppymatylhdrazxytixtwwqqqlrcusxyxzymrnryyernrxbgrphsioxrxhmxwzsytmhnosnrpwtphaunprdtbpwapgjjqcnykgspjsxslxztfsuflijbeebwyyowjzpsbjcdabxmxhtweppffglvhfloprfavduzbgkw'))
53 changes: 53 additions & 0 deletions leetcode/str_to_int_atoi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class Solution:
def myAtoi(self, s: str) -> int:
result = s.strip()

sign = ''

if not result:
return 0
if result[0] in ['-', '+']:
sign = result[0]
if len(result) > 1 and not str.isdigit(result[1]):
return 0 # '+-12' case

digit: str = result[1:]
elif not str.isdigit(result[0]):
return 0
elif str.isdigit(result[0]):

digit: str = result


digits = ''.join(list(filter(lambda s: str.isdigit(s) or s == '.', digit)))
if digits not in digit:
first_alpha = list(filter(lambda s: str.isalpha(s) or s in ['+', ' ', '-'], digit))
if first_alpha:
first_alpha = first_alpha[0]

idx = digit.index(first_alpha)
digit = digit[0:idx].strip().split()[0]
else:
digit = digit
# return 0
else:
digit = digits
try:
output = int(sign + digit)
except ValueError:
try:
output = int(float(sign + digit))
except ValueError:
return 0

if output >= 2 ** 32 // 2:
return 2 ** 32 // 2 -1
elif output < -2 ** 32 // 2:
return -2 ** 32 // 2
else:
return output


if __name__ == '__main__':
s = Solution()
print(s.myAtoi(" 123 456"))
27 changes: 27 additions & 0 deletions leetcode/zigzag_conversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution:
def convert(self, s: str, numRows: int) -> str:
m = [[''] * len(s) for i in range(numRows)]
global_idx = 0
column = 0
while global_idx < len(s):

for row_idx in range(numRows):
m[row_idx][column] = s[global_idx]
global_idx += 1
if global_idx >= len(s):
break
if global_idx < len(s):
for rox_idx_up in range(numRows - 2):
column += 1
m[numRows - rox_idx_up - 2][column] = s[global_idx]
global_idx += 1
if global_idx >= len(s):
break
column += 1

return ''.join([m[r][c] for r in range(len(m)) for c in range(len(m[0]))])


if __name__ == '__main__':
s = Solution()
print(s.convert("PAYPALISHIRING", 3))

0 comments on commit a558e17

Please sign in to comment.