Skip to content

Commit 3c1c0b7

Browse files
committed
6.07
1 parent d56b0b3 commit 3c1c0b7

12 files changed

+256
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
3+
只要有一组相同的数,他们的下表之差小于等于k,就返回True,题目理解有误,不过仍然使用字典保存结果
4+
5+
"""
6+
7+
"""这里要注意一个细节,判断value是否在res中时,如果时判断是否在res.keys()中,会超时"""
8+
9+
class Solution(object):
10+
def containsNearbyDuplicate(self, nums, k):
11+
"""
12+
:type nums: List[int]
13+
:type k: int
14+
:rtype: bool
15+
"""
16+
17+
res = dict()
18+
for index, value in enumerate(nums):
19+
if value in res and index - res[value] <= k:
20+
return True
21+
res[value] = index
22+
return False

easy/二进制/231_Power of Two.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
3+
Given an integer, write a function to determine if it is a power of two.
4+
5+
Credits:
6+
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
7+
8+
"""
9+
"""
10+
判断一个数是否是2的此方数,只需判断是否只有1位二进制位是1即可,用之前提到的方法,n与n-1的按位与可以把最右边一位1变为0。
11+
"""
12+
class Solution(object):
13+
def isPowerOfTwo(self, n):
14+
"""
15+
:type n: int
16+
:rtype: bool
17+
"""
18+
return n>0 and not (n & n-1)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
3+
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
4+
5+
"""
6+
7+
"""my solution"""
8+
9+
class Solution(object):
10+
def containsDuplicate(self, nums):
11+
"""
12+
:type nums: List[int]
13+
:rtype: bool
14+
"""
15+
return len(nums) != len(set(nums))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""
2+
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
3+
只要有一组相同的数,他们的下表之差小于等于k,就返回True,题目理解有误,不过仍然使用字典保存结果
4+
5+
"""
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""
2+
Implement the following operations of a stack using queues.
3+
4+
push(x) -- Push element x onto stack.
5+
pop() -- Removes the element on top of the stack.
6+
top() -- Get the top element.
7+
empty() -- Return whether the stack is empty.
8+
Notes:
9+
You must use only standard operations of a queue -- which means only push to back, peek/pop from front, size, and is empty operations are valid.
10+
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
11+
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
12+
13+
"""
14+
"""solution:使用collections提供的deque数据结构"""
15+
import collections
16+
class MyStack(object):
17+
def __init__(self):
18+
"""
19+
Initialize your data structure here.
20+
"""
21+
self.stack = collections.deque([])
22+
23+
def push(self, x):
24+
"""
25+
Push element x onto stack.
26+
:type x: int
27+
:rtype: void
28+
"""
29+
self.stack.append(x)
30+
31+
def pop(self):
32+
"""
33+
Removes the element on top of the stack and returns that element.
34+
:rtype: int
35+
"""
36+
for i in range(len(self.stack) - 1):
37+
self.stack.append(self.stack.popleft())
38+
return self.stack.popleft()
39+
40+
def top(self):
41+
"""
42+
Get the top element.
43+
:rtype: int
44+
"""
45+
return self.stack[-1]
46+
47+
def empty(self):
48+
"""
49+
Returns whether the stack is empty.
50+
:rtype: bool
51+
"""
52+
return len(self.stack) == 0
53+
54+
55+
56+
# Your MyStack object will be instantiated and called as such:
57+
# obj = MyStack()
58+
# obj.push(x)
59+
# param_2 = obj.pop()
60+
# param_3 = obj.top()
61+
# param_4 = obj.empty()
File renamed without changes.

easy/树/226_Invert Binary Tree.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
3+
Invert a binary tree.
4+
5+
4
6+
/ \
7+
2 7
8+
/ \ / \
9+
1 3 6 9
10+
to
11+
4
12+
/ \
13+
7 2
14+
/ \ / \
15+
9 6 3 1
16+
17+
"""
18+
"""my solution
19+
递归的思想,如果左子树不为空,则交换左子树,如果右子树不为空,则交换右子树
20+
"""
21+
# Definition for a binary tree node.
22+
# class TreeNode(object):
23+
# def __init__(self, x):
24+
# self.val = x
25+
# self.left = None
26+
# self.right = None
27+
28+
class Solution(object):
29+
def invertTree(self, root):
30+
"""
31+
:type root: TreeNode
32+
:rtype: TreeNode
33+
"""
34+
if not root:
35+
return None
36+
if root.left != None:
37+
self.invertTree(root.left)
38+
if root.right != None:
39+
self.invertTree(root.right)
40+
root.right,root.left = root.left,root.right
41+
return root
42+
43+
"""更简单的方法
44+
python函数默认返回一个None
45+
"""
46+
47+
# Definition for a binary tree node.
48+
# class TreeNode(object):
49+
# def __init__(self, x):
50+
# self.val = x
51+
# self.left = None
52+
# self.right = None
53+
54+
class Solution(object):
55+
def invertTree(self, root):
56+
"""
57+
:type root: TreeNode
58+
:rtype: TreeNode
59+
"""
60+
if root:
61+
root.left, root.right = self.invertTree(root.right), self.invertTree(root.left)
62+
return root

0 commit comments

Comments
 (0)