KF Tirana (4-3-3)
Portiere: Enea Selmani
<|vq_11371|><|repo_name|>RohitRajmohan/DS<|file_sep|>/leetcode/longest_palindromic_substring.py
class Solution(object):
def longest_palindrome(self,s):
if not s:
return ""
if len(s) ==1:
return s
start =0
max_length =1
for i in range(0,len(s)-1):
len1 = self.expand_from_center(s,i,i)
len2 = self.expand_from_center(s,i,i+1)
max_len = max(len1,len2)
if max_len > max_length:
start = i-(max_len -1)/2
max_length = max_len
return s[start:start+max_length]
def expand_from_center(self,s,left,right):
while left >=0 and right< len(s) and s[left] == s[right]:
left -=1
right +=1
return right -left -1
if __name__ == "__main__":
sol = Solution()
print sol.longest_palindrome("abba")<|repo_name|>RohitRajmohan/DS<|file_sep|>/leetcode/permutations.py
class Solution(object):
def permute(self,nums):
result = []
self.helper(nums,result,[],len(nums))
return result
def helper(self,num,result,path,length):
if len(path) == length:
result.append(path[:])
return
for i in range(0,len(num)):
if num[i] not in path:
path.append(num[i])
self.helper(num,result,path,length)
path.pop()
if __name__ == "__main__":
sol = Solution()
print sol.permute([1,2])<|file_sep|># Definition for singly-linked list.
class ListNode(object):
def __init__(self,x):
self.val = x
self.next = None
class Solution(object):
def mergeKLists(self,lists):
if not lists:
return None
length = len(lists)
while length >1:
k = length /2 + length %2
for i in range(0,k):
lists[i] = self.mergeTwoLists(lists[2*i],lists[2*i+1])
length = k
return lists[0]
def mergeTwoLists(self,l1,l2):
if not l1:
return l2
if not l2:
return l1
head = ListNode(-1)
tail = head
while l1 and l2:
if l1.val <= l2.val:
tail.next = l1
l1 = l1.next
else:
tail.next = l2
l2 = l2.next
tail= tail.next
if not l1:
tail.next= l2
if not l2:
tail.next= l1
<|file_sep|># Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def reverseList(self,head):
if not head or not head.next:
return head
pivot=head.next
<|repo_name|>RohitRajmohan/DS<|file_sep|>/leetcode/add_two_numbers.py
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def addTwoNumbers(self,l1,l2):
head=ListNode(0)
current=head
carry=0
<|repo_name|>RohitRajmohan/DS<|file_sep|>/leetcode/find_min_rotated_sorted_array.py
class Solution(object):
def findMin(self,num):
if __name__ == "__main__":
sol = Solution()
print sol.findMin([5,6,7])<|file_sep|># Definition for singly-linked list.
class ListNode(object):
def __init__(self,x):
self.val=x
self.next=None
class Solution(object):
def mergeTwoLists(self,l1,l2):
if __name__ == "__main__":
sol=Solution()
l11=ListNode(1)
l12=ListNode(5)
l13=ListNode(8)
l11.next=l12;
l12.next=l13;
l21=ListNode(6)
l22=ListNode(9)
l21.next=l22;
print sol.mergeTwoLists(l11,l21)<|repo_name|>RohitRajmohan/DS<|file_sep|>/leetcode/search_in_rotated_sorted_array.py
class Solution(object):
def search(self,A,target):
if __name__ == "__main__":
sol=Solution()
print sol.search([6,7,8,9],6)<|repo_name|>RohitRajmohan/DS<|file_sep|>/leetcode/reverse_integer.py
class Solution(object):
def reverse(self,x):
if __name__ == "__main__":
sol=Solution()
print sol.reverse(123)<|repo_name|>RohitRajmohan/DS<|file_sep|>/leetcode/zigzag_conversion.py
class Solution(object):
def convert(self,s,rings):
if __name__ == "__main__":
sol=Solution()
print sol.convert("PAYPALISHIRING",5)<|file_sep|># Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def buildTree(self,inorder,self.index,self.preorder,self.preindex,self.size):
if __name__ == "__main__":
sol=Solution()<|repo_name|>RohitRajmohan/DS<|file_sep|>/leetcode/palindrome_number.py
class Solution(object):
def isPalindrome(self,x):
if __name__ == "__main__":
sol=Solution()
print sol.isPalindrome(-121)<|repo_name|>RohitRajmohan/DS<|file_sep|>/leetcode/merge_intervals.py
import collections
class Interval(object):
def __init__(self,s,e):
class Solution(object):
if __name__ == "__main__":
sol=Solution()<|repo_name|>RohitRajmohan/DS<|file_sep|>/leetcode/search_in_rotated_sorted_array_ii.py
class Solution(object):
def search(self,A,target):
if __name__ == "__main__":
sol=Solution()
print sol.search([5],5)<|repo_name|>RohitRajmohan/DS<|file_sep|>/leetcode/binary_tree_zigzag_level_order_traversal.py
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
if __name__ == "__main__":
sol=Solution()<|repo_name|>RohitRajmohan/DS<|file_sep|>/leetcode/add_binary.py
def addBinary(a,b):
return bin(int(a,2)+int(b))
print addBinary("11","111")
print addBinary("10","101")<|repo_name|>RohitRajmohan/DS<|file_sep|>/README.md
Some data structures and algorithms implemented in Python.
[](https://www.codacy.com/app/Rohit Rajmohan/DS?utm_source=github.com&utm_medium=referral&utm_content=RohitRajmohan/DS&utm_campaign=Badge_Grade)
### Algorithms implemented
* Merge Sort
* Quick Sort
* Binary Search
* Fibonacci Numbers
* Knapsack Problem
* Longest Common Subsequence
* Tower of Hanoi
### Data Structures implemented
* Stack
* Queue
* Linked List
* Hash Table
### Leetcode Problems Solved
* Two Sum Problem
* Best Time to Buy and Sell Stock Problem
* Best Time to Buy and Sell Stock II Problem
* Reverse Integer
* Palindrome Number
* Search Insert Position
* Climbing Stairs
* Maximum Subarray Problem
* Valid Parentheses
* Valid Anagram
* Longest Common Prefix
* Regular Expression Matching
* Implement strStr()
* Valid Sudoku
* Generate Parentheses
* Letter Combinations of a Phone Number
* Remove Duplicates from Sorted Array
* Remove Element
* Move Zeroes
* Merge Sorted Array
* Plus One
* Rotate Array
* Merge Two Sorted Lists
* Add Two Numbers
* Median of Two Sorted Arrays
* Search in Rotated Sorted Array
* Search for a Range
* Reverse Integer
* Remove Duplicates from Sorted Array II
* Linked List Cycle II
* Merge k Sorted Lists
#### TODO: Complete rest of the problems <|repo_name|>RohitRajmohan/DS<|file_sep