Skip to content

Commit d1f5468

Browse files
add 3 leetcodes
1 parent c23ce0e commit d1f5468

File tree

5 files changed

+173
-49
lines changed

5 files changed

+173
-49
lines changed

.idea/workspace.xml

Lines changed: 86 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Target Offer/链表中环的入口结点.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ def MeetingNode(self, pHead):
1616
if pSlow == None:
1717
return None
1818
pFast = pSlow.next
19-
while pSlow != None and pFast != None:
19+
while pFast:
2020
if pSlow == pFast:
2121
return pSlow
2222
pSlow = pSlow.next
2323
pFast = pFast.next
24-
if pFast != None:
24+
if pFast:
2525
pFast = pFast.next
2626

2727
def EntryNodeOfLoop(self, pHead):

leetcode/101. Symmetric Tree.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'''
2+
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
3+
4+
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
5+
6+
1
7+
/ \
8+
2 2
9+
/ \ / \
10+
3 4 4 3
11+
But the following [1,2,2,null,3,null,3] is not:
12+
1
13+
/ \
14+
2 2
15+
\ \
16+
3 3
17+
'''
18+
19+
class TreeNode(object):
20+
def __init__(self, x):
21+
self.val = x
22+
self.left = None
23+
self.right = None
24+
25+
class Solution(object):
26+
# recursive
27+
def isSymmetric(self, root):
28+
return self.selfIsSymmetrical(root, root)
29+
def selfIsSymmetrical(self, pRoot1, pRoot2):
30+
if pRoot1 and pRoot2:
31+
return pRoot1.val == pRoot2.val and self.selfIsSymmetrical(pRoot1.left, pRoot2.right) and self.selfIsSymmetrical(pRoot1.right, pRoot2.left)
32+
else:
33+
return pRoot1 == pRoot2
34+
35+
#iterative(BFS)
36+
def isSymmetric2(self, root):
37+
if root:
38+
now = [root]
39+
while now:
40+
vals = [i.val if i else None for i in now]
41+
if list(reversed(vals)) != vals:
42+
return False
43+
else:
44+
now = [j for i in now if i for j in (i.left, i.right)]
45+
return True
46+
47+
# iterative(DFS)
48+
def isSymmetric3(self, root):
49+
if root:
50+
stack = [(root.left, root.right)]
51+
while len(stack) > 0:
52+
p, q = stack.pop()
53+
if p and q and p.val == q.val:
54+
stack.append((p.left, q.right))
55+
stack.append((p.right, q.left))
56+
elif p != q:
57+
return False
58+
return True
59+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'''
2+
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
3+
4+
For example,
5+
Given 1->2->3->3->4->4->5, return 1->2->5.
6+
Given 1->1->1->2->3, return 2->3.
7+
'''
8+
# -*- coding:utf-8 -*-
9+
class ListNode:
10+
def __init__(self, x):
11+
self.val = x
12+
self.next = None
13+
class Solution:
14+
def deleteDuplication(self, head):
15+
dummy = pre = ListNode(0)
16+
dummy.next = head
17+
while head and head.next:
18+
if head.val == head.next.val:
19+
while head and head.next and head.val == head.next.val:
20+
head = head.next
21+
head = head.next
22+
pre.next = head
23+
else:
24+
pre = pre.next
25+
head = head.next
26+
return dummy.next

leetcode/83. Remove Duplicates from Sorted List.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ def __init__(self, x):
1414

1515
class Solution(object):
1616
def deleteDuplicates(self, head):
17-
"""
18-
:type head: ListNode
19-
:rtype: ListNode
20-
"""
2117
pNode = head
2218
while pNode:
2319
while pNode.next and pNode.next.val == pNode.val:

0 commit comments

Comments
 (0)