Skip to content

Commit c5e78e2

Browse files
committed
Add solutions
1 parent c9c87ba commit c5e78e2

File tree

13 files changed

+394
-0
lines changed

13 files changed

+394
-0
lines changed

Algorithms/01 Matrix/01-matrix.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Source : https://leetcode.com/problems/01-matrix/#/description
2+
// Author : Han Zichi
3+
// Date : 2016-03-27
4+
5+
/**
6+
* @param {number[][]} matrix
7+
* @return {number[][]}
8+
*/
9+
var updateMatrix = function(matrix) {
10+
// BFS
11+
let q = [];
12+
let hash = [];
13+
let [m, n] = [matrix.length, matrix[0].length];
14+
const dir = [[1, 0], [-1, 0], [0, 1], [0, -1]];
15+
16+
for (let i = 0; i < m; i++) {
17+
hash[i] = [];
18+
for (let j = 0; j < n; j++) {
19+
if (matrix[i][j] === 0) {
20+
q.push({x: i, y: j, step: 0});
21+
hash[i][j] = 0;
22+
}
23+
}
24+
}
25+
26+
while (q.length) {
27+
let item = q.shift();
28+
let {x, y, step} = item;
29+
30+
for (let i = 0; i < 4; i++) {
31+
let _x = x + dir[i][0];
32+
let _y = y + dir[i][1];
33+
if (_x < 0 || _x >= m || _y < 0 || _y >= n) continue;
34+
if (hash[_x][_y] !== undefined) continue;
35+
hash[_x][_y] = step + 1;
36+
q.push({x: _x, y: _y, step: step + 1});
37+
}
38+
}
39+
40+
return hash;
41+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Source : https://leetcode.com/problems/complex-number-multiplication/#/description
2+
# Author : Han Zichi
3+
# Date : 2017-04-23
4+
5+
class Solution(object):
6+
def complexNumberMultiply(self, a, b):
7+
"""
8+
:type a: str
9+
:type b: str
10+
:rtype: str
11+
"""
12+
a = [int(i) for i in a[:-1].split('+')]
13+
b = [int(i) for i in b[:-1].split('+')]
14+
c = a[0] * b[0] - a[1] * b[1]
15+
d = a[0] * b[1] + a[1] * b[0]
16+
return str(c) + '+' + str(d) + 'i'
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Source : https://leetcode.com/problems/convert-bst-to-greater-tree/#/description
2+
# Author : Han Zichi
3+
# Date : 2017-04-23
4+
5+
# Definition for a binary tree node.
6+
# class TreeNode(object):
7+
# def __init__(self, x):
8+
# self.val = x
9+
# self.left = None
10+
# self.right = None
11+
12+
class Solution(object):
13+
def convertBST(self, root):
14+
"""
15+
:type root: TreeNode
16+
:rtype: TreeNode
17+
"""
18+
def dfs(node):
19+
if node == None:
20+
return
21+
else:
22+
dfs(node.right)
23+
node.val += dfs.total
24+
dfs.total = node.val
25+
dfs(node.left)
26+
27+
dfs.total = 0
28+
dfs(root)
29+
return root
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Source : https://leetcode.com/problems/diameter-of-binary-tree/#/description
2+
# Author : Han Zichi
3+
# Date : 2017-04-23
4+
5+
# Definition for a binary tree node.
6+
# class TreeNode(object):
7+
# def __init__(self, x):
8+
# self.val = x
9+
# self.left = None
10+
# self.right = None
11+
12+
class Solution(object):
13+
def diameterOfBinaryTree(self, root):
14+
"""
15+
:type root: TreeNode
16+
:rtype: int
17+
"""
18+
19+
20+
def getHeight(node, depth):
21+
if node == None:
22+
return -1
23+
else:
24+
left = getHeight(node.left, depth) + 1
25+
right = getHeight(node.right, depth) + 1
26+
getHeight.ans = max(getHeight.ans, left + right)
27+
return max(left, right)
28+
29+
getHeight.ans = 0
30+
getHeight(root, 0)
31+
32+
return getHeight.ans
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Source : https://leetcode.com/problems/encode-and-decode-tinyurl/
2+
// Author : Han Zichi
3+
// Date : 2017-04-23
4+
5+
let [p, index] = [new Map(), 0];
6+
7+
var base62 = (n) => {
8+
let str = '0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ';
9+
let len = str.length;
10+
let ret = '';
11+
12+
do {
13+
ret += str[n % len];
14+
n = ~~(n / len);
15+
} while (n);
16+
17+
return ret;
18+
};
19+
20+
/**
21+
* Encodes a URL to a shortened URL.
22+
*
23+
* @param {string} longUrl
24+
* @return {string}
25+
*/
26+
var encode = function(longUrl) {
27+
let shortUrl = base62(index++);
28+
p.set(shortUrl, longUrl);
29+
return shortUrl;
30+
};
31+
32+
/**
33+
* Decodes a shortened URL to its original URL.
34+
*
35+
* @param {string} shortUrl
36+
* @return {string}
37+
*/
38+
var decode = function(shortUrl) {
39+
return p.get(shortUrl);
40+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Source : https://leetcode.com/problems/friend-circles/#/description
2+
# Author : Han Zichi
3+
# Date : 2017-04-23
4+
5+
class Solution(object):
6+
def findCircleNum(self, M):
7+
"""
8+
:type M: List[List[int]]
9+
:rtype: int
10+
"""
11+
def dfs(index):
12+
hash[index] = True
13+
for index, item in enumerate(M[index]):
14+
if hash[index] == False and item == 1:
15+
dfs(index)
16+
17+
n = len(M)
18+
ans = 0
19+
hash = [False] * n
20+
21+
for i in range(n):
22+
if hash[i] == False:
23+
ans += 1
24+
dfs(i)
25+
26+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Source : https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/#/description
2+
# Author : Han Zichi
3+
# Date : 2017-04-23
4+
5+
import random
6+
class RandomizedCollection(object):
7+
8+
def __init__(self):
9+
"""
10+
Initialize your data structure here.
11+
"""
12+
self.s = []
13+
14+
15+
def insert(self, val):
16+
"""
17+
Inserts a value to the collection. Returns true if the collection did not already contain the specified element.
18+
:type val: int
19+
:rtype: bool
20+
"""
21+
if val in self.s:
22+
self.s.append(val)
23+
return False
24+
else:
25+
self.s.append(val)
26+
return True
27+
28+
29+
def remove(self, val):
30+
"""
31+
Removes a value from the collection. Returns true if the collection contained the specified element.
32+
:type val: int
33+
:rtype: bool
34+
"""
35+
if val in self.s:
36+
self.s.remove(val)
37+
return True
38+
else:
39+
return False
40+
41+
def getRandom(self):
42+
"""
43+
Get a random element from the collection.
44+
:rtype: int
45+
"""
46+
index = int(random.random() * len(self.s))
47+
return self.s[index]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Source : https://leetcode.com/problems/insert-delete-getrandom-o1/#/description
2+
# Author : Han Zichi
3+
# Date : 2017-04-23
4+
5+
import random
6+
class RandomizedSet(object):
7+
8+
def __init__(self):
9+
"""
10+
Initialize your data structure here.
11+
"""
12+
self.s = set()
13+
14+
15+
def insert(self, val):
16+
"""
17+
Inserts a value to the set. Returns true if the set did not already contain the specified element.
18+
:type val: int
19+
:rtype: bool
20+
"""
21+
if val in self.s:
22+
return False
23+
else:
24+
self.s.add(val)
25+
return True
26+
27+
28+
def remove(self, val):
29+
"""
30+
Removes a value from the set. Returns true if the set contained the specified element.
31+
:type val: int
32+
:rtype: bool
33+
"""
34+
if val in self.s:
35+
self.s.remove(val)
36+
return True
37+
else:
38+
return False
39+
40+
41+
def getRandom(self):
42+
"""
43+
Get a random element from the set.
44+
:rtype: int
45+
"""
46+
lis = list(self.s)
47+
index = int(random.random() * len(lis))
48+
return lis[index]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Source : https://leetcode.com/problems/longest-uncommon-subsequence-i/#/description
2+
# Author : Han Zichi
3+
# Date : 2017-04-23
4+
5+
class Solution(object):
6+
def findLUSlength(self, a, b):
7+
"""
8+
:type a: str
9+
:type b: str
10+
:rtype: int
11+
"""
12+
if a == b:
13+
return -1
14+
if a.find(b) != -1:
15+
return len(a)
16+
if b.find(a) != -1:
17+
return len(b)
18+
return max(len(a), len(b))
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Source : https://leetcode.com/problems/minimum-time-difference/#/description
2+
// Author : Han Zichi
3+
// Date : 2016-03-26
4+
5+
/**
6+
* @param {string[]} timePoints
7+
* @return {number}
8+
*/
9+
var findMinDifference = function(timePoints) {
10+
let helper = str => {
11+
let arr = str.split(':');
12+
return +arr[0] * 60 + (+arr[1]);
13+
};
14+
15+
timePoints = timePoints.map(helper);
16+
timePoints = [...timePoints, ...timePoints.map(item => item + 60 * 24)];
17+
timePoints.sort((a, b) => a - b);
18+
19+
let ans = Infinity;
20+
for (let i = 1, len = timePoints.length; i < len; i++)
21+
ans = Math.min(ans, timePoints[i] - timePoints[i - 1]);
22+
23+
return ans;
24+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Source : https://leetcode.com/problems/perfect-number/#/description
2+
# Author : Han Zichi
3+
# Date : 2017-04-23
4+
5+
import math
6+
class Solution(object):
7+
def checkPerfectNumber(self, num):
8+
"""
9+
:type num: int
10+
:rtype: bool
11+
"""
12+
if num <= 1:
13+
return False
14+
15+
total = 1
16+
end = int(math.sqrt(num))
17+
for i in range(2, end + 1):
18+
if num % i == 0:
19+
total += i + num / i
20+
21+
return total == num
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Source : https://leetcode.com/problems/random-pick-index/#/description
2+
# Author : Han Zichi
3+
# Date : 2017-04-23
4+
5+
import random
6+
class Solution(object):
7+
8+
def __init__(self, nums):
9+
"""
10+
11+
:type nums: List[int]
12+
:type numsSize: int
13+
"""
14+
self.nums = nums
15+
16+
def pick(self, target):
17+
"""
18+
:type target: int
19+
:rtype: int
20+
"""
21+
array = []
22+
for index, item in enumerate(self.nums):
23+
if item == target:
24+
array.append(index)
25+
26+
index = int(random.random() * len(array))
27+
return array[index]

0 commit comments

Comments
 (0)