Skip to content

Commit

Permalink
2019-06-27
Browse files Browse the repository at this point in the history
  • Loading branch information
JiayangWu committed Jun 27, 2019
1 parent 19c1978 commit 86cbc47
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 0 deletions.
49 changes: 49 additions & 0 deletions 0143.重排链表/0143-重排链表.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution(object):
def reorderList(self, head):
"""
:type head: ListNode
:rtype: None Do not return anything, modify head in-place instead.
"""
if not head or not head.next:
return head

slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next

l1, l2 = head, self.reverseList(slow.next)
slow.next = None
# self.printList(l1)
# self.printList(l2)
while l1 and l2:
cur = l2
l2 = l2.next

cur.next = l1.next
l1.next = cur
l1 = l1.next.next

return head

def reverseList(self, head):
if not head or not head.next:
return head
p = self.reverseList(head.next)
head.next.next = head
head.next = None
return p

def printList(self, head):
l = []
p = head
while p:
l.append(p.val)
p = p.next
print l
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution(object):
def insertionSortList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head or not head.next:
return head

dummy = ListNode(-1)
dummy.next = head
pre = head #pre始终指着排序好链表的最后一个节点
cur = head.next #cur始终指着未排序链表的第一个节点
while cur:
tail = cur.next
pre.next = tail #把cur这个节点拿出来

p = dummy
while p.next and p.next.val < cur.val: #找到插入的位置
p = p.next

cur.next = p.next #把cur插入到p和p.next之间
p.next = cur
cur = tail

if p == pre:#如果刚插入到了已排序链表的末尾
pre = pre.next #那么就更新pre
return dummy.next
42 changes: 42 additions & 0 deletions 0148.排序链表/0148-排序链表.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution(object):
def sortList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""

if not head or not head.next:
return head
pre, slow, fast = head, head, head
while fast and fast.next:
pre = slow
slow = slow.next
fast = fast.next.next

pre.next = None
left, right = self.sortList(head), self.sortList(slow)
return self.merge(left, right)

def merge(self, l1, l2):
if not l1:
return l2
if not l2:
return l1

if l1.val < l2.val:
head = ListNode(l1.val)
head.next = self.merge(l1.next, l2)
else:
head = ListNode(l2.val)
head.next = self.merge(l1, l2.next)
return head




31 changes: 31 additions & 0 deletions 0156.上下翻转二叉树/0156-上下翻转二叉树.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution(object):
def upsideDownBinaryTree(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
#每一个节点变成其左孩子的右节点
if not root or (not root.left and not root.right):
return root

parent, sibling = None, None
while root:
tmp = root.left
root.left = sibling

sibling = root.right
root.right = parent

parent = root
root = tmp

return parent


36 changes: 36 additions & 0 deletions 0157.用Read4读取N个字符/0157-用Read4读取N个字符.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""
The read4 API is already defined for you.
@param buf, a list of characters
@return an integer
def read4(buf):
# Below is an example of how the read4 API can be called.
file = File("abcdefghijk") # File is "abcdefghijk", initially file pointer (fp) points to 'a'
buf = [' '] * 4 # Create buffer with enough space to store characters
read4(buf) # read4 returns 4. Now buf = ['a','b','c','d'], fp points to 'e'
read4(buf) # read4 returns 4. Now buf = ['e','f','g','h'], fp points to 'i'
read4(buf) # read4 returns 3. Now buf = ['i','j','k',...], fp points to end of file
"""
class Solution(object):
def read(self, buf, n):
"""
:type buf: Destination buffer (List[str])
:type n: Number of characters to read (int)
:rtype: The number of actual characters read (int)
"""
tmp = ["","","",""]
cnt = 0
read4(tmp)
while tmp != ["","","",""]:
for i in range(4):
if tmp[i]:
buf[cnt] = tmp[i]
cnt += 1
if cnt == n + 1:
return n
tmp = ["","","",""]
read4(tmp)
return cnt


10 changes: 10 additions & 0 deletions 0190.颠倒二进制位/0190-颠倒二进制位.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution:
# @param n, an integer
# @return an integer
def reverseBits(self, n):
b = bin(n)[2:]
b = "0" * (32 - len(b)) + b
# print b
# print b[::-1]
return int(b[::-1], 2)

0 comments on commit 86cbc47

Please sign in to comment.