Skip to content

Commit 4a2263d

Browse files
committed
more algorithms
1 parent 2342aa1 commit 4a2263d

File tree

8 files changed

+173
-0
lines changed

8 files changed

+173
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ go to home directory
1717

1818
#### compile class with maven
1919
mvn compile
20+
21+
22+
#### how to run python, at corresponding directory and type following
23+
python filename

python/Factorial.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def factorial(n):
2+
if n==1:
3+
return 1
4+
else:
5+
return n * factorial(n-1)
6+
7+
print(factorial(3))
8+

python/FindLargestValue.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
def largestValues(root):
3+
if(root==None):
4+
return []
5+
6+
que = [root]
7+
re = []
8+
9+
while len(que)!=0:
10+
size = len(que)
11+
tmax = float('-inf')
12+
13+
for i in range(size):
14+
t = que.pop(0)
15+
tmax = max(tmax, t.val)
16+
17+
if(t.left != None):
18+
que.append(t.left)
19+
if(t.right != None):
20+
que.append(t.right)
21+
22+
re.append(tmax)
23+
return re
24+
25+
26+
class TreeNode(object):
27+
def __init__(self, val=0, left=None, right=None):
28+
self.val = val
29+
self.left = left
30+
self.right = right
31+
32+
n1 = TreeNode(1)
33+
n2 = TreeNode(2)
34+
n3 = TreeNode(3)
35+
n1.left = n3
36+
n1.right = n2
37+
n3.left = TreeNode(5)
38+
n3.right = TreeNode(3)
39+
n2.right = TreeNode(9)
40+
41+
print(largestValues(n1))

python/KeysAndRooms.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def do(rooms, index, visited):
2+
for i in rooms[index]:
3+
if(not visited[i]):
4+
visited[i] = True
5+
do(rooms, i, visited)
6+
7+
8+
def canVisitAllRooms(rooms):
9+
visited = [False] * len(rooms)
10+
visited[0] = True
11+
do(rooms, 0, visited)
12+
13+
for i in visited:
14+
if not i:
15+
return False
16+
return True
17+
18+
19+
print(canVisitAllRooms([[1],[2],[3],[]]))

python/LowestCommonAncesstor.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class TreeNode(object):
2+
def __init__(self, val=0, left=None, right=None):
3+
self.val = val
4+
self.left = left
5+
self.right = right
6+
7+
n1 = TreeNode(1)
8+
n2 = TreeNode(2)
9+
n3 = TreeNode(3)
10+
n1.left = n3
11+
n1.right = n2
12+
n3.left = TreeNode(5)
13+
n3.right = TreeNode(6)
14+
n2.right = TreeNode(9)
15+
16+
17+
def lowestCommonAncesstor(root, p, q):
18+
if root==None or root==p or root==q:
19+
return root
20+
l = lowestCommonAncesstor(root.left, p, q)
21+
r = lowestCommonAncesstor(root.right, p, q)
22+
23+
if l==None:
24+
return r
25+
if r==None:
26+
return l
27+
return root
28+
29+
30+
print(lowestCommonAncesstor(n1, 2, 3))

python/MaxBinaryTree.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class TreeNode(object):
2+
def __init__(self, val=0, left=None, right=None):
3+
self.val = val
4+
self.left = left
5+
self.right = right
6+
7+
def treePrint(root):
8+
if root!=None:
9+
print(root.val)
10+
treePrint(root.left)
11+
treePrint(root.right)
12+
13+
def constructMaximumBinaryTree(nums):
14+
stack = []
15+
for i in range(len(nums)):
16+
cur = TreeNode(nums[i])
17+
while len(stack)>0 and stack[-1].val < nums[i]:
18+
cur.left = stack.pop()
19+
if(len(stack)>0):
20+
stack[-1].right = cur
21+
stack.append(cur)
22+
return None if len(stack) == 0 else stack[0]
23+
24+
25+
nums = [3,2,1,6,0,5]
26+
tree = constructMaximumBinaryTree(nums)
27+
treePrint(tree)

python/structure/HashTest.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class DateKey:
2+
def __init__(self, name, id):
3+
self.__name = name
4+
self.__id = id
5+
6+
def __hash__(self):
7+
prime = 31;
8+
result = 1;
9+
result = prime * result + self.__id;
10+
result = prime * result + 0 if self.__name == None else id(self.__name);
11+
return result;
12+
13+
def __eq__(self, other):
14+
if id(self) == id(other):
15+
return True
16+
if other==None:
17+
return False
18+
if not isinstance(other, DateKey):
19+
return False
20+
if self.__id != other.__id:
21+
return False
22+
return self.__name == other.__name
23+
24+
25+
26+
27+
a = DateKey("Tom1", 123)
28+
dic = {}
29+
dic[a] = 10
30+
31+
b = DateKey("Tom1", 123)
32+
print(b in dic)
33+
34+

python/test.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
No one is unaware of the name of that famous English shipowner, Cunard.
2+
In 1840 this shrewd industrialist founded a postal service between Liverpool and Halifax, featuring three wooden ships with 400-horsepower paddle wheels and a burden of 1,162 metric tons.
3+
Eight years later, the company's assets were increased by four 650-horsepower ships at 1,820 metric tons, and in two more years, by two other vessels of still greater power and tonnage.
4+
In 1853 the Cunard Co., whose mail-carrying charter had just been renewed, successively added to its assets the Arabia, the Persia, the China, the Scotia, the Java, and the Russia, all ships of top speed and, after the Great Eastern, the biggest ever to plow the seas.
5+
So in 1867 this company owned twelve ships, eight with paddle wheels and four with propellers.
6+
If I give these highly condensed details, it is so everyone can fully understand the importance of this maritime transportation company, known the world over for its shrewd management.
7+
No transoceanic navigational undertaking has been conducted with more ability, no business dealings have been crowned with greater success.
8+
In twenty-six years Cunard ships have made 2,000 Atlantic crossings without so much as a voyage canceled, a delay recorded, a man, a craft, or even a letter lost.
9+
Accordingly, despite strong competition from France, passengers still choose the Cunard line in preference to all others, as can be seen in a recent survey of official documents.
10+
Given this, no one will be astonished at the uproar provoked by this accident involving one of its finest steamers.

0 commit comments

Comments
 (0)