Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd1f5468

Browse files
add 3 leetcodes
1 parentc23ce0e commitd1f5468

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 aboutcustomizing 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
ifpSlow==None:
1717
returnNone
1818
pFast=pSlow.next
19-
whilepSlow!=NoneandpFast!=None:
19+
whilepFast:
2020
ifpSlow==pFast:
2121
returnpSlow
2222
pSlow=pSlow.next
2323
pFast=pFast.next
24-
ifpFast!=None:
24+
ifpFast:
2525
pFast=pFast.next
2626

2727
defEntryNodeOfLoop(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+
classTreeNode(object):
20+
def__init__(self,x):
21+
self.val=x
22+
self.left=None
23+
self.right=None
24+
25+
classSolution(object):
26+
# recursive
27+
defisSymmetric(self,root):
28+
returnself.selfIsSymmetrical(root,root)
29+
defselfIsSymmetrical(self,pRoot1,pRoot2):
30+
ifpRoot1andpRoot2:
31+
returnpRoot1.val==pRoot2.valandself.selfIsSymmetrical(pRoot1.left,pRoot2.right)andself.selfIsSymmetrical(pRoot1.right,pRoot2.left)
32+
else:
33+
returnpRoot1==pRoot2
34+
35+
#iterative(BFS)
36+
defisSymmetric2(self,root):
37+
ifroot:
38+
now= [root]
39+
whilenow:
40+
vals= [i.valifielseNoneforiinnow]
41+
iflist(reversed(vals))!=vals:
42+
returnFalse
43+
else:
44+
now= [jforiinnowififorjin (i.left,i.right)]
45+
returnTrue
46+
47+
# iterative(DFS)
48+
defisSymmetric3(self,root):
49+
ifroot:
50+
stack= [(root.left,root.right)]
51+
whilelen(stack)>0:
52+
p,q=stack.pop()
53+
ifpandqandp.val==q.val:
54+
stack.append((p.left,q.right))
55+
stack.append((p.right,q.left))
56+
elifp!=q:
57+
returnFalse
58+
returnTrue
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+
classListNode:
10+
def__init__(self,x):
11+
self.val=x
12+
self.next=None
13+
classSolution:
14+
defdeleteDuplication(self,head):
15+
dummy=pre=ListNode(0)
16+
dummy.next=head
17+
whileheadandhead.next:
18+
ifhead.val==head.next.val:
19+
whileheadandhead.nextandhead.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+
returndummy.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
classSolution(object):
1616
defdeleteDuplicates(self,head):
17-
"""
18-
:type head: ListNode
19-
:rtype: ListNode
20-
"""
2117
pNode=head
2218
whilepNode:
2319
whilepNode.nextandpNode.next.val==pNode.val:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp