Skip to content

Commit

Permalink
add leetocde
Browse files Browse the repository at this point in the history
  • Loading branch information
RochaC authored and chenzh committed Dec 22, 2020
1 parent da7b33a commit 75c2ad6
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 1 deletion.
41 changes: 41 additions & 0 deletions 19_RemoveNthNodeFromEndofList.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
" File Description:
"
"
" Created by Rocha(chenzhihao) on 2020/12/17.
" Mail: [email protected]
"""


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


class Solution(object):
def removeNthFromEnd(self, head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
dummy_node = ListNode(0)
dummy_node.next = head

fast = dummy_node
slow = dummy_node


for i in range(n):
fast = fast.next

while fast.next:
fast = fast.next
slow = slow.next

slow.next = slow.next.next

return dummy_node.next

33 changes: 33 additions & 0 deletions 21_MergeTwoSortedLists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
" File Description:
" Merge two sorted linked lists and return it as a new sorted list.
The new list should be made by splicing together the nodes of the first two lists.
"
" Created by Rocha(chenzhihao) on 2020/12/17.
" Mail: [email protected]
"""


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


class Solution(object):
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if not l1: return l2
if not l2: return l1

if l1.val > l2.val:
l2.next = self.mergeTwoLists(l1, l2.next)
return l2
else:
l1.next = self.mergeTwoLists(l1.next, l2)
return l1
21 changes: 21 additions & 0 deletions 34_FindFirstandLastPositionofElementinSortedArray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
" File Description:
" Given an array of integers nums sorted in ascending order,
find the starting and ending position of a given target value.
If target is not found in the array, return [-1, -1].
Follow up: Could you write an algorithm with O(log n) runtime complexity?
"
" Created by Rocha(chenzhihao) on 2020/12/17.
" Mail: [email protected]
"""


class Solution(object):
def searchRange(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
56 changes: 56 additions & 0 deletions 39_Combination_Sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
" File Description:
"
"
" Created by Rocha(chenzhihao) on 2020/6/26.
"""


class Solution(object):
def __getDp(self, candidates, sum):
if not self.dp[sum]:
self.dp[sum] = []
for i in candidates:
left = sum - i
if (left == 0):
self.dp[sum].append([i])
return self.dp[sum]

elif (left > 0):
ok = False
if not self.dp[left]:
self.__getDp(candidates, left)
if self.dp[left]:
ok = True

if(ok):
for comb in self.dp[left]:
tmp = comb.copy()
tmp.append(i)
tmp = sorted(tmp)
if tmp not in self.dp[sum]:
self.dp[sum].append(tmp)
else:
return None
else:
return self.dp[sum]


def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
self.dp = [None] * (target+1)
candidates = sorted(candidates)

self.__getDp(candidates, target)

return self.dp[target]

if __name__ == '__main__':
candidates = [2, 3, 5]
target = 8
so = Solution()
print(so.combinationSum(candidates, target))
37 changes: 37 additions & 0 deletions 39_Combination_Sum2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Solution(object):
def combinationSum2(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
self.res = []
self.sorted_candidates = sorted(candidates)
self.len = len(self.sorted_candidates)
self.dfs(target, 0, [])

return self.res

def dfs(self,target, pos_start, vlist):
print("Search target: %s, Pos start: %s, vlist: %s" %(target, pos_start, vlist))
if target == 0:
self.res.append(vlist)
return
elif target < 0:
print("break")
return True
else:
for i in range(pos_start, self.len):
if(i > pos_start and self.sorted_candidates[i] == self.sorted_candidates[i-1]):
continue # remove duplicate

if_break = self.dfs(target-self.sorted_candidates[i], i+1, vlist + [self.sorted_candidates[i]])
if if_break:
break


if __name__ == '__main__':
candidates = [2,5,2,1,2]
target = 5
so = Solution()
print(so.combinationSum2(candidates, target))
2 changes: 1 addition & 1 deletion 55_JumpGame.py

Large diffs are not rendered by default.

47 changes: 47 additions & 0 deletions 7_reverse_integer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
" File Description:
" Given a 32-bit signed integer, reverse digits of an integer.
Note:
Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range:
[−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
"
" Created by Rocha(chenzhihao) on 2020/12/15.
" Mail: [email protected]
"""

import sys

class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
res = 0
neg = False
maxint = 2**31 -1
minint = - 2**31

if x < 0:
neg = True
x = abs(x)

while x != 0:
res = (x % 10 + res * 10)
x //= 10

if not neg and res > maxint:
return 0
elif neg and res > abs(maxint):
return 0

if neg:
res = -res
return res

if __name__ == '__main__':
so = Solution()
print(so.reverse(1534236469))

0 comments on commit 75c2ad6

Please sign in to comment.