Leetcode刷题记录_20181028

225. Implement Stack using Queues 

实现栈的操作方法。通过列表方法实现。

 1 class MyStack(object):
 2 
 3     def __init__(self):
 4         """
 5         Initialize your data structure here.
 6         """
 7         self.stack = []
 8         
 9 
10     def push(self, x):
11         """
12         Push element x onto stack.
13         :type x: int
14         :rtype: void
15         """
16         self.stack += [x]
17         
18 
19     def pop(self):
20         """
21         Removes the element on top of the stack and returns that element.
22         :rtype: int
23         """
24         return self.stack.pop()
25         
26 
27     def top(self):
28         """
29         Get the top element.
30         :rtype: int
31         """
32         return self.stack[-1]
33 
34     def empty(self):
35         """
36         Returns whether the stack is empty.
37         :rtype: bool
38         """
39         return len(self.stack) == 0
View Code

226. Invert Binary Tree

递归。

class Solution:
    def invertTree(self, root):
        """
        :type root: TreeNode
        :rtype: TreeNode
        """
        if not root:
            return None
        else:
            root.left,root.right= root.right,root.left
            self.invertTree(root.left)
            self.invertTree(root.right)
        return root
View Code

231、Power of Two  

判断是否为2的乘方值。

 1 class Solution:
 2     def isPowerOfTwo(self, n):
 3         """
 4         :type n: int
 5         :rtype: bool
 6         """
 7         if n ==0:
 8             return False
 9         if n == 1:
10             return True
11         
12         while n:
13             if n ==2:
14                 return True
15             if n%2 ==0:
16                 n = n/2
17             else:
18                 return False
19         return True
View Code
 1 class Solution(object):
 2     def isPowerOfTwo(self, n):
 3         """
 4         :type n: int
 5         :rtype: bool
 6         """
 7         if n <1:
 8             return False
 9         while (n&1 == 0):
10             n >>=1
11         return n == 1
排名第一的算法

使用位运算方法

n&1 == 0:判断奇偶数 0偶数 1基数

n >>=1: 按位右移1位,相当于除以2

232. Implement Queue using Stacks

 1 class MyQueue:
 2 
 3     def __init__(self):
 4         """
 5         Initialize your data structure here.
 6         """
 7         self.queue = []
 8 
 9     def push(self, x):
10         """
11         Push element x to the back of queue.
12         :type x: int
13         :rtype: void
14         """
15         self.queue = [x] + self.queue
16         
17 
18     def pop(self):
19         """
20         Removes the element from in front of queue and returns that element.
21         :rtype: int
22         """
23         a =  self.queue[-1]
24         self.queue.pop(-1)
25         return a
26 
27     def peek(self):
28         """
29         Get the front element.
30         :rtype: int
31         """
32         return self.queue[-1]
33         
34 
35     def empty(self):
36         """
37         Returns whether the queue is empty.
38         :rtype: bool
39         """
40         return len(self.queue) == 0
View Code

234. Palindrome Linked List

判断链表是否是回文链表

 1 class Solution:
 2     def isPalindrome(self, head):
 3         """
 4         :type head: ListNode
 5         :rtype: bool
 6         """
 7         if not head:
 8             return True
 9         temp = []
10         while head:
11             temp.append(head.val)
12             head = head.next
13         
14         temp1 = temp[::-1]
15     
16         return temp1 == temp
View Code

注意: list.reverse() 是在对列表本身进行修改,无返回值

 

posted @ 2018-10-28 23:48  adminyzz  阅读(117)  评论(0编辑  收藏  举报