Skip to content

Commit 86cbc47

Browse files
committedJun 27, 2019
2019-06-27
1 parent 19c1978 commit 86cbc47

File tree

6 files changed

+202
-0
lines changed

6 files changed

+202
-0
lines changed
 
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Definition for singly-linked list.
2+
# class ListNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution(object):
8+
def reorderList(self, head):
9+
"""
10+
:type head: ListNode
11+
:rtype: None Do not return anything, modify head in-place instead.
12+
"""
13+
if not head or not head.next:
14+
return head
15+
16+
slow, fast = head, head
17+
while fast and fast.next:
18+
slow = slow.next
19+
fast = fast.next.next
20+
21+
l1, l2 = head, self.reverseList(slow.next)
22+
slow.next = None
23+
# self.printList(l1)
24+
# self.printList(l2)
25+
while l1 and l2:
26+
cur = l2
27+
l2 = l2.next
28+
29+
cur.next = l1.next
30+
l1.next = cur
31+
l1 = l1.next.next
32+
33+
return head
34+
35+
def reverseList(self, head):
36+
if not head or not head.next:
37+
return head
38+
p = self.reverseList(head.next)
39+
head.next.next = head
40+
head.next = None
41+
return p
42+
43+
def printList(self, head):
44+
l = []
45+
p = head
46+
while p:
47+
l.append(p.val)
48+
p = p.next
49+
print l
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Definition for singly-linked list.
2+
# class ListNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution(object):
8+
def insertionSortList(self, head):
9+
"""
10+
:type head: ListNode
11+
:rtype: ListNode
12+
"""
13+
if not head or not head.next:
14+
return head
15+
16+
dummy = ListNode(-1)
17+
dummy.next = head
18+
pre = head #pre始终指着排序好链表的最后一个节点
19+
cur = head.next #cur始终指着未排序链表的第一个节点
20+
while cur:
21+
tail = cur.next
22+
pre.next = tail #把cur这个节点拿出来
23+
24+
p = dummy
25+
while p.next and p.next.val < cur.val: #找到插入的位置
26+
p = p.next
27+
28+
cur.next = p.next #把cur插入到p和p.next之间
29+
p.next = cur
30+
cur = tail
31+
32+
if p == pre:#如果刚插入到了已排序链表的末尾
33+
pre = pre.next #那么就更新pre
34+
return dummy.next
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Definition for singly-linked list.
2+
# class ListNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution(object):
8+
def sortList(self, head):
9+
"""
10+
:type head: ListNode
11+
:rtype: ListNode
12+
"""
13+
14+
if not head or not head.next:
15+
return head
16+
pre, slow, fast = head, head, head
17+
while fast and fast.next:
18+
pre = slow
19+
slow = slow.next
20+
fast = fast.next.next
21+
22+
pre.next = None
23+
left, right = self.sortList(head), self.sortList(slow)
24+
return self.merge(left, right)
25+
26+
def merge(self, l1, l2):
27+
if not l1:
28+
return l2
29+
if not l2:
30+
return l1
31+
32+
if l1.val < l2.val:
33+
head = ListNode(l1.val)
34+
head.next = self.merge(l1.next, l2)
35+
else:
36+
head = ListNode(l2.val)
37+
head.next = self.merge(l1, l2.next)
38+
return head
39+
40+
41+
42+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def upsideDownBinaryTree(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: TreeNode
13+
"""
14+
#每一个节点变成其左孩子的右节点
15+
if not root or (not root.left and not root.right):
16+
return root
17+
18+
parent, sibling = None, None
19+
while root:
20+
tmp = root.left
21+
root.left = sibling
22+
23+
sibling = root.right
24+
root.right = parent
25+
26+
parent = root
27+
root = tmp
28+
29+
return parent
30+
31+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
The read4 API is already defined for you.
3+
4+
@param buf, a list of characters
5+
@return an integer
6+
def read4(buf):
7+
8+
# Below is an example of how the read4 API can be called.
9+
file = File("abcdefghijk") # File is "abcdefghijk", initially file pointer (fp) points to 'a'
10+
buf = [' '] * 4 # Create buffer with enough space to store characters
11+
read4(buf) # read4 returns 4. Now buf = ['a','b','c','d'], fp points to 'e'
12+
read4(buf) # read4 returns 4. Now buf = ['e','f','g','h'], fp points to 'i'
13+
read4(buf) # read4 returns 3. Now buf = ['i','j','k',...], fp points to end of file
14+
"""
15+
class Solution(object):
16+
def read(self, buf, n):
17+
"""
18+
:type buf: Destination buffer (List[str])
19+
:type n: Number of characters to read (int)
20+
:rtype: The number of actual characters read (int)
21+
"""
22+
tmp = ["","","",""]
23+
cnt = 0
24+
read4(tmp)
25+
while tmp != ["","","",""]:
26+
for i in range(4):
27+
if tmp[i]:
28+
buf[cnt] = tmp[i]
29+
cnt += 1
30+
if cnt == n + 1:
31+
return n
32+
tmp = ["","","",""]
33+
read4(tmp)
34+
return cnt
35+
36+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
# @param n, an integer
3+
# @return an integer
4+
def reverseBits(self, n):
5+
b = bin(n)[2:]
6+
b = "0" * (32 - len(b)) + b
7+
# print b
8+
# print b[::-1]
9+
return int(b[::-1], 2)
10+

0 commit comments

Comments
 (0)
Please sign in to comment.