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

Commit ec3551d

Browse files
add some code
1 parent f791e2f commit ec3551d

12 files changed

+324
-0
lines changed

‎python/19_RemoveNthNodeFromEndofList.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,32 @@
55
# @File : 19_RemoveNthNodeFromEndofList.py
66
# @Package : LeetCode
77
# @E-mail : charlotte77_hu@sina.com
8+
9+
# Definition for singly-linked list.
10+
# class ListNode(object):
11+
# def __init__(self, x):
12+
# self.val = x
13+
# self.next = None
14+
15+
class Solution(object):
16+
def removeNthFromEnd(self, head, n):
17+
"""
18+
:type head: ListNode
19+
:type n: int
20+
:rtype: ListNode
21+
"""
22+
new_head = ListNode(0)
23+
new_head.next =head
24+
fast=slow=new_head
25+
for i in range(n+1):
26+
fast = fast.next
27+
while fast:
28+
fast=fast.next
29+
slow=slow.next
30+
slow.next=slow.next.next
31+
return new_head.next
32+
33+
34+
if __name__=='__main__':
35+
s = Solution()
36+
print s.removeNthFromEnd([1,2,3,4,5],2)

‎python/20_validParentheses.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,24 @@
55
# @File : 20_validParentheses.py
66
# @Package : LeetCode
77
# @E-mail : charlotte77_hu@sina.com
8+
9+
class Solution(object):
10+
def isValid(self, s):
11+
"""
12+
:type s: str
13+
:rtype: bool
14+
"""
15+
pars = [None]
16+
#这个字典建立的顺序也很重要
17+
parmap = {')':'(','}':'{',']':'['}
18+
for c in s:
19+
if c in parmap:
20+
if parmap[c] !=pars.pop():
21+
return False
22+
else:
23+
pars.append(c)
24+
return len(pars) == 1
25+
26+
if __name__=='__main__':
27+
s = Solution()
28+
print s.isValid(")(")

‎python/21_MergeTwoSortedLists.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,30 @@
55
# @File : 21_MergeTwoSortedLists.py
66
# @Package : LeetCode
77
# @E-mail : charlotte77_hu@sina.com
8+
9+
# Definition for singly-linked list.
10+
# class ListNode(object):
11+
# def __init__(self, x):
12+
# self.val = x
13+
# self.next = None
14+
15+
class Solution(object):
16+
def mergeTwoLists(self, l1, l2):
17+
"""
18+
:type l1: ListNode
19+
:type l2: ListNode
20+
:rtype: ListNode
21+
"""
22+
if not l1 or not l2:
23+
return l1 or l2
24+
head = cur = ListNode(0)
25+
while l1 and l2:
26+
if l1.val <l2.val:
27+
cur.next = l1
28+
l1 = l1.next
29+
else:
30+
cur.next = l2
31+
l2 = l2.next
32+
cur = cur.next
33+
cur.next = l1 or l2
34+
return head.next

‎python/22_GenerateParentheses.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,22 @@
55
# @File : 22_GenerateParentheses.py
66
# @Package : LeetCode
77
# @E-mail : charlotte77_hu@sina.com
8+
9+
10+
class Solution(object):
11+
def generateParenthesis(self, n):
12+
"""
13+
:type n: int
14+
:rtype: List[str]
15+
"""
16+
def generate(p,left,right):
17+
if right >= left >=0:
18+
if not right:
19+
yield p
20+
for q in generate(p+'(' ,left-1,right):yield q
21+
for q in generate(p+')',left,right -1):yield q
22+
return list(generate('',n,n))
23+
24+
if __name__ == "__main__":
25+
s = Solution()
26+
print s.generateParenthesis(3)

‎python/23_MergeKSortedLists.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,19 @@
55
# @File : 23_MergeKSortedLists.py
66
# @Package : LeetCode
77
# @E-mail : charlotte77_hu@sina.com
8+
9+
10+
from Queue import PriorityQueue
11+
12+
class Solution(object):
13+
def mergeKLists(self, lists):
14+
dummy = ListNode(None)
15+
curr = dummy
16+
q = PriorityQueue()
17+
for node in lists:
18+
if node: q.put((node.val,node))
19+
while q.qsize()>0:
20+
curr.next = q.get()[1]
21+
curr=curr.next
22+
if curr.next: q.put((curr.next.val, curr.next))
23+
return dummy.next

‎python/24_SwapNodesInPairs.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,27 @@
55
# @File : 24_SwapNodesInPairs.py
66
# @Package : LeetCode
77
# @E-mail : charlotte77_hu@sina.com
8+
9+
# Definition for singly-linked list.
10+
# class ListNode(object):
11+
# def __init__(self, x):
12+
# self.val = x
13+
# self.next = None
14+
15+
class Solution(object):
16+
def swapPairs(self, head):
17+
"""
18+
:type head: ListNode
19+
:rtype: ListNode
20+
"""
21+
if not head or not head.next:
22+
return head
23+
pre = new_head = ListNode(0)
24+
while head and head.next:
25+
tmp = head.next
26+
head.next = tmp.next
27+
tmp.next = head
28+
pre.next = tmp
29+
pre = head
30+
head = head.next
31+
return new_head.next

‎python/25_ReverseNodesinK-Group.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,32 @@
55
# @File : 25_ReverseNodesinK-Group.py
66
# @Package : LeetCode
77
# @E-mail : charlotte77_hu@sina.com
8+
9+
# Definition for singly-linked list.
10+
# class ListNode(object):
11+
# def __init__(self, x):
12+
# self.val = x
13+
# self.next = None
14+
15+
class Solution(object):
16+
def reverseKGroup(self, head, k):
17+
"""
18+
:type head: ListNode
19+
:type k: int
20+
:rtype: ListNode
21+
"""
22+
dummy = jump = ListNode(0)
23+
dummy.next = l = r = head
24+
25+
while True:
26+
count = 0
27+
while r and count < k:
28+
r = r.next
29+
count += 1
30+
if count == k:
31+
pre, cur = r, l
32+
for _ in range(k):
33+
cur.next, cur, pre = pre, cur.next, cur
34+
jump.next, jump, l = pre, l, r
35+
else:
36+
return dummy.next

‎python/26_RemoveDuplicatesfromSortedArray.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,44 @@
55
# @File : 26_RemoveDuplicatesfromSortedArray.py
66
# @Package : LeetCode
77
# @E-mail : charlotte77_hu@sina.com
8+
9+
class Solution1(object):
10+
def removeDuplicates(self, nums):
11+
"""
12+
:type nums: List[int]
13+
:rtype: int
14+
"""
15+
# 方法一:用两个指针,一个指针用于扫描遍历整个列表,
16+
# 另一个指针时终止向下一个数字要写入列表的位置,
17+
# 效果相当于在便利列表的时候,将不同的数字重新写入到原数组
18+
19+
if len(nums)==0:
20+
return 0
21+
cur=0
22+
for i in range(1,len(nums)):
23+
if nums[i] != nums[cur]:
24+
cur +=1
25+
nums[cur] = nums[i]
26+
return cur+1
27+
28+
class Solution2(object):
29+
def removeDuplicates(self, nums):
30+
"""
31+
:type nums: List[int]
32+
:rtype: int
33+
"""
34+
# 方法二:用一个计数器记录当前有多少重复数字,
35+
# 以此来决定下一个要写入数组的数字的位置,
36+
# 以及当便利玩数组时得到的新长度
37+
count = 0
38+
for i in range(1,len(nums)):
39+
if nums[i] == nums[i-1]:
40+
count +=1
41+
else:
42+
nums[i-count] = nums[i]
43+
return len(nums)-count
44+
45+
46+
if __name__=='__main__':
47+
s = Solution2()
48+
print s.removeDuplicates([1,1,2])

‎python/27_RemoveElement.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,26 @@
55
# @File : 27_RemoveElement.py
66
# @Package : LeetCode
77
# @E-mail : charlotte77_hu@sina.com
8+
9+
10+
class Solution(object):
11+
def removeElement(self, nums, val):
12+
"""
13+
:type nums: List[int]
14+
:type val: int
15+
:rtype: int
16+
"""
17+
# 方法一:用两个指针,一个指针顺序扫描素有元素判断当前元素是否需要删除,另一个指针一直指向下一个不是要删除元素的位置
18+
# 这样相当于把原数组中要删除的数去除后,所有其他数字前移
19+
20+
length = 0
21+
22+
for i in xrange(len(nums)):
23+
if nums[i] !=val:
24+
nums[length] =nums[i]
25+
length += 1
26+
return length
27+
28+
if __name__=='__main__':
29+
s = Solution()
30+
print s.removeElement([2,3,3,2],3)

‎python/28_ImplementstrStr().py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,26 @@
55
# @File : 28_ImplementstrStr().py
66
# @Package : LeetCode
77
# @E-mail : charlotte77_hu@sina.com
8+
9+
10+
class Solution(object):
11+
def strStr(self, haystack, needle):
12+
"""
13+
:type haystack: str
14+
:type needle: str
15+
:rtype: int
16+
"""
17+
if not needle:
18+
return 0
19+
for i in xrange(len(haystack) - len(needle)+1):
20+
if haystack[i] == needle[0]:
21+
j = 1
22+
while j < len(needle) and haystack[i+j] ==needle[j]:
23+
j+=1
24+
if j ==len(needle):
25+
return i
26+
return -1
27+
28+
if __name__=='__main__':
29+
s = Solution()
30+
print s.strStr('hello','ll')

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /