Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 281a901

Browse files
committed
add a code directory
1 parent 4d46f34 commit 281a901

6 files changed

+181
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cells": [],
3+
"metadata": {},
4+
"nbformat": 4,
5+
"nbformat_minor": 2
6+
}

code/1.two-sum.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#
2+
# [1] Two Sum
3+
#
4+
# https://leetcode.com/problems/two-sum
5+
#
6+
# Easy (32.90%)
7+
# Total Accepted: 498409
8+
# Total Submissions: 1515036
9+
# Testcase Example: '[3,2,4]\n6'
10+
#
11+
# Given an array of integers, return indices of the two numbers such that they
12+
# add up to a specific target.
13+
#
14+
# You may assume that each input would have exactly one solution, and you may
15+
# not use the same element twice.
16+
#
17+
#
18+
# Example:
19+
#
20+
# Given nums = [2, 7, 11, 15], target = 9,
21+
#
22+
# Because nums[0] + nums[1] = 2 + 7 = 9,
23+
# return [0, 1].
24+
#
25+
#
26+
#
27+
class Solution(object):
28+
def twoSum(self, nums, target):
29+
"""
30+
:type nums: List[int]
31+
:type target: int
32+
:rtype: List[int]
33+
"""
34+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
from collections import deque
8+
class Solution(object):
9+
def levelOrder(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: List[List[int]]
13+
"""
14+
ll=[]
15+
if root==None:
16+
return ll
17+
q=deque([])
18+
q.append(root)
19+
20+
while len(q):
21+
size=len(q)
22+
level_list=[]
23+
for i in xrange(size):
24+
node=q.popleft()
25+
level_list.append(node.val)
26+
if node.left:
27+
q.append(node.left)
28+
if node.right:
29+
q.append(node.right)
30+
ll.append(level_list)
31+
return ll
32+
33+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 maxDepth(self,root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: int
13+
"""
14+
"""
15+
每次只需要求左右子树高度的最大值,加上1,就是树的高度
16+
"""
17+
if root==None:
18+
return 0
19+
left_depth=self.maxDepth(root.left)
20+
right_depth=self.maxDepth(root.right)
21+
return max(left_depth,right_depth) + 1
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
"""
10+
采用递归方式,每次获得左右子树的深度,判断左右子树是否满足平衡条件
11+
使用辅助的函数获得树的深度
12+
此种方法中有重复递归
13+
"""
14+
"""
15+
def isBalanced(self, root):
16+
if root==None:
17+
return True
18+
left_depth=self.maxDepth(root.left)
19+
right_depth=self.maxDepth(root.right)
20+
diff=left_depth-right_depth
21+
if abs(diff)>1:
22+
return False
23+
return self.isBalanced(root.left) and self.isBalanced(root.right)
24+
25+
26+
27+
def maxDepth(self,root):
28+
if root==None:
29+
return 0
30+
return max(self.maxDepth(root.left),self.maxDepth(root.right))+1
31+
32+
"""
33+
34+
"""
35+
可以将获得深度和判断平衡放在一起,因为都是类似后序遍历,在获得子对象的信息之后再做相应操作
36+
"""
37+
def isBalanced(self,root):
38+
39+
return self.solve(root)[1]
40+
41+
def solve(self,root):
42+
if root==None:
43+
return [0,True]
44+
tmp1_left=self.solve(root.left)
45+
tmp2_right=self.solve(root.right)
46+
depth=max(tmp1_left[0],tmp2_right[0])+1
47+
48+
if abs(tmp1_left[0]-tmp2_right[0])>1:
49+
return [depth,False]
50+
else:
51+
return [depth,tmp1_left[1] and tmp2_right[1]]
52+
53+
54+
55+
56+
57+
58+
59+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 minDepth(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: int
13+
"""
14+
"""
15+
求最小深度和求最大深度相似,但是注意斜树的情况。
16+
当左侧子树深度为0时,返回右侧子树的深度+1
17+
当右侧子树深度为0时,返回左侧子树的深度+1
18+
"""
19+
if root==None:
20+
return 0
21+
left_depth=self.minDepth(root.left)
22+
right_depth=self.minDepth(root.right)
23+
if left_depth==0:
24+
return right_depth+1
25+
if right_depth==0:
26+
return left_depth+1
27+
return min(left_depth,right_depth)+1
28+

0 commit comments

Comments
 (0)