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
11 changes: 11 additions & 0 deletions PARTICIPANTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,15 @@
- 📫 Reach me: **[email protected]**
- 🔭 Connect with me: **[Piyushjar](https://github.com/piyushjar))**

---

### Connect with me:

<img align="right" src="https://avatars3.githubusercontent.com/Vaishnvee-shinde?size=100" width="100px;" alt=" Vaishnvee Shinde"/>

- 👨‍💻 My name is **Vaishnvee Shinde**
- 🌱 I’m a Full Stack Developer Developer.
- 📫 Reach me: **[email protected]**
- 🔭 Connect with me: **[Piyushjar](https://github.com/Vaishnvee-Shinde))**

---
27 changes: 27 additions & 0 deletions python/101_Symmetric_Tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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 isSymmetric(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if root is None:
return True
return self.mirrorVisit(root.left, root.right)

def mirrorVisit(self, left, right):
if left is None and right is None:
return True
try:
if left.val == right.val:
if self.mirrorVisit(left.left, right.right) and self.mirrorVisit(left.right, right.left):
return True
return False
except:
return False
66 changes: 66 additions & 0 deletions python/python/001_Two_Sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
class Solution(object):
# def twoSum(self, nums, target):
# """
# :type nums: List[int]
# :type target: int
# :rtype: List[int]
# """
# #n^2
# ls = len(nums)
# for i in range(ls):
# for j in range(i + 1, ls):
# if nums[i] + nums[j] == target:
# return [i, j]

# def twoSum(self, nums, target):
# # hash 1
# hash_nums = {}
# for index, num in enumerate(nums):
# try:
# hash_nums[num].append(index)
# except KeyError:
# hash_nums[num] = [index]
# for index, num in enumerate(nums):
# another = target - num
# try:
# candicate = hash_nums[another]
# if another == num:
# if len(candicate) > 1:
# return candicate
# else:
# continue
# else:
# return [index, candicate[0]]
# except KeyError:
# pass

# def twoSum(self, nums, target):
# # hash 2
# hash_nums = {}
# for index, num in enumerate(nums):
# another = target - num
# try:
# hash_nums[another]
# return [hash_nums[another], index]
# except KeyError:
# hash_nums[num] = index

def twoSum(self, nums, target):
# two point
nums_index = [(v, index) for index, v in enumerate(nums)]
nums_index.sort()
begin, end = 0, len(nums) - 1
while begin < end:
curr = nums_index[begin][0] + nums_index[end][0]
if curr == target:
return [nums_index[begin][1], nums_index[end][1]]
elif curr < target:
begin += 1
else:
end -= 1


if __name__ == '__main__':
# begin
s = Solution()
print s.twoSum([3, 2, 4], 6)
57 changes: 57 additions & 0 deletions python/python/002_Add_Two_Numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# 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):
# """
# :type l1: ListNode
# :type l2: ListNode
# :rtype: ListNode
# """
# last = 0
# head = prev = None
# while True:
# if l2 is None and l1 is None and last == 0:
# break
# val = last
# if l2 is not None:
# val += l2.val
# l2 = l2.next
# if l1 is not None:
# val += l1.val
# l1 = l1.next
# if val >= 10:
# val = val % 10
# last = 1
# else:
# last = 0
# current = ListNode(val)
# if prev is None:
# head = current
# else:
# prev.next = current
# prev = current
# return head

def addTwoNumbers(self, l1, l2):
carry = 0
# dummy head
head = curr = ListNode(0)
while l1 or l2:
val = carry
if l1:
val += l1.val
l1 = l1.next
if l2:
val += l2.val
l2 = l2.next
curr.next = ListNode(val % 10)
curr = curr.next
carry = int(val / 10)
if carry > 0:
curr.next = ListNode(carry)
return head.next
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
class Solution(object):
# def lengthOfLongestSubstring(self, s):
# """
# :type s: str
# :rtype: int
# """
# sls = len(set(s))
# ls = len(s)
# if ls >= 1:
# max_l = 1
# else:
# return 0
# for i in range(ls):
# for j in range(i + max_l + 1, i + sls + 1):
# curr = s[i:j]
# c_ls = len(curr)
# if len(set(curr)) != c_ls:
# break
# else:
# if c_ls > max_l:
# max_l = c_ls
# if max_l == sls:
# return sls
# return max_l

# def lengthOfLongestSubstring(self, s):
# sls = len(set(s))
# ls = len(s)
# if ls >= 1:
# max_l = 1
# else:
# return 0
# for i in range(ls):
# for j in reversed(range(i + max_l + 1, i + sls + 1)):
# curr = s[i:j]
# c_ls = len(curr)
# if len(set(curr)) == c_ls:
# if c_ls > max_l:
# max_l = c_ls
# if max_l == sls:
# return sls
# break
# return max_l

# def lengthOfLongestSubstring(self, s):
# exist = [False] * 256
# ls = len(s)
# max_len = i = 0
# for j in range(ls):
# while(exist[ord(s[j])]):
# exist[ord(s[i])] = False
# i += 1
# exist[ord(s[j)] = True
# max_len = max(max_len, j - i + 1)
# return max_len

def lengthOfLongestSubstring(self, s):
# https://leetcode.com/articles/longest-substring-without-repeating-characters/
charMap = {}
for i in range(256):
charMap[i] = -1
ls = len(s)
i = max_len = 0
for j in range(ls):
# Note that when charMap[ord(s[j])] >= i, it means that there are
# duplicate character in current i,j. So we need to update i.
if charMap[ord(s[j])] >= i:
i = charMap[ord(s[j])] + 1
charMap[ord(s[j])] = j
max_len = max(max_len, j - i + 1)
return max_len
60 changes: 60 additions & 0 deletions python/python/004_Median_of_Two_Sorted_Arrays.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
class Solution(object):
# def findMedianSortedArrays(self, nums1, nums2):
# """
# :type nums1: List[int]
# :type nums2: List[int]
# :rtype: float
# """
# p1 = p2 = 0
# ls1 = len(nums1)
# ls2 = len(nums2)
# all_nums = []
# median = 0.0
# while p1 < ls1 and p2 < ls2:
# if nums1[p1] < nums2[p2]:
# all_nums.append(nums1[p1])
# p1 += 1
# else:
# all_nums.append(nums2[p2])
# p2 += 1
# if p1 < ls1:
# while p1 < ls1:
# all_nums.append(nums1[p1])
# p1 += 1
# if p2 < ls2:
# while p2 < ls2:
# all_nums.append(nums2[p2])
# p2 += 1
# # print all_nums
# if (ls1 + ls2) % 2 == 1:
# median = all_nums[(ls1 + ls2) / 2]
# else:
# median = 1.0 * (all_nums[(ls1 + ls2) / 2] + all_nums[(ls1 + ls2) / 2 - 1]) / 2
# return median

def findMedianSortedArrays(self, nums1, nums2):
# https://discuss.leetcode.com/topic/4996/share-my-o-log-min-m-n-solution-with-explanation
# https://discuss.leetcode.com/topic/16797/very-concise-o-log-min-m-n-iterative-solution-with-detailed-explanation
ls1, ls2 = len(nums1), len(nums2)
if ls1 < ls2:
return self.findMedianSortedArrays(nums2, nums1)
l, r = 0, ls2 * 2
while l <= r:
mid2 = (l + r) >> 1
mid1 = ls1 + ls2 - mid2
L1 = -sys.maxint - 1 if mid1 == 0 else nums1[(mid1 - 1) >> 1]
L2 = -sys.maxint - 1 if mid2 == 0 else nums2[(mid2 - 1) >> 1]
R1 = sys.maxint if mid1 == 2 * ls1 else nums1[mid1 >> 1]
R2 = sys.maxint if mid2 == 2 * ls2 else nums2[mid2 >> 1]
if L1 > R2:
l = mid2 + 1
elif L2 > R1:
r = mid2 - 1
else:
return (max(L1, L2) + min(R1, R2)) / 2.0


if __name__ == '__main__':
# begin
s = Solution()
print s.findMedianSortedArrays([1, 1], [1, 2])
88 changes: 88 additions & 0 deletions python/python/005_Longest_Palindromic_Substring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
# my solution
# expand string according to Manacher algorithm
# but extend radius step by step
ls = len(s)
if ls <= 1 or len(set(s)) == 1:
return s
# create a new list like this: "abc"->"a#b#c"
temp_s = '#'.join('{}'.format(s))
# print temp_s
tls = len(temp_s)
seed = range(1, tls - 1)
# this table stores the max length palindrome
len_table = [0] * tls
for step in range(1, tls / 2 + 1):
final = []
for pos in seed:
if pos - step < 0 or pos + step >= tls:
continue
if temp_s[pos - step] != temp_s[pos + step]:
continue
final.append(pos)
if temp_s[pos - step] == '#':
continue
len_table[pos] = step
seed = final
max_pos, max_step = 0, 0
for i, s in enumerate(len_table):
if s >= max_step:
max_step = s
max_pos = i
return temp_s[max_pos - max_step:max_pos + max_step + 1].translate(None, '#')

# def longestPalindrome(self, s):
# # example in leetcode book
# max_left, max_right = 0, 0
# ls = len(s)
# for i in range(ls):
# len1 = self.expandAroundCenter(s, i, i)
# len2 = self.expandAroundCenter(s, i, i + 1)
# max_len = max(len1, len2)
# if (max_len > max_right - max_left):
# max_left = i - (max_len - 1) / 2
# max_right = i + max_len / 2
# return s[max_left:max_right + 1]
#
# def expandAroundCenter(self, s, left, right):
# ls = len(s)
# while (left >= 0 and right < ls and s[left] == s[right]):
# left -= 1
# right += 1
# return right - left - 1

# def longestPalindrome(self, s):
# #Manacher algorithm
# #http://en.wikipedia.org/wiki/Longest_palindromic_substring
# # Transform S into T.
# # For example, S = "abba", T = "^#a#b#b#a#$".
# # ^ and $ signs are sentinels appended to each end to avoid bounds checking
# T = '#'.join('^{}$'.format(s))
# n = len(T)
# P = [0] * n
# C = R = 0
# for i in range (1, n-1):
# P[i] = (R > i) and min(R - i, P[2*C - i]) # equals to i' = C - (i-C)
# # Attempt to expand palindrome centered at i
# while T[i + 1 + P[i]] == T[i - 1 - P[i]]:
# P[i] += 1
#
# # If palindrome centered at i expand past R,
# # adjust center based on expanded palindrome.
# if i + P[i] > R:
# C, R = i, i + P[i]
#
# # Find the maximum element in P.
# maxLen, centerIndex = max((n, i) for i, n in enumerate(P))
# return s[(centerIndex - maxLen)//2: (centerIndex + maxLen)//2]


if __name__ == '__main__':
# begin
s = Solution()
print(s.longestPalindrome("abcbe"))
Loading