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

Commit 92149c6

Browse files
committed
Add my solutions up to #695
1 parent 2727a79 commit 92149c6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1363
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @param {TreeNode} p
11+
* @param {TreeNode} q
12+
* @return {TreeNode}
13+
*/
14+
var lowestCommonAncestor = function(root, p, q) {
15+
let parent_value = root.val;
16+
let p_value = p.val;
17+
let q_value = q.val;
18+
if(p_value > parent_value && q_value > parent_value){
19+
return lowestCommonAncestor(root.right,p,q)
20+
} else if(p_value < parent_value && q_value < parent_value){
21+
return lowestCommonAncestor(root.left,p,q)
22+
} else{
23+
return root
24+
}
25+
26+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @param {TreeNode} p
11+
* @param {TreeNode} q
12+
* @return {TreeNode}
13+
*/
14+
var lowestCommonAncestor = function(root, p, q) {
15+
let result = [];
16+
function helper(node){
17+
if(!node){
18+
return false;
19+
}
20+
let left = helper(node.left);
21+
let right = helper(node.right);
22+
let middle = node == p || node == q;
23+
if(left + right + middle >= 2){
24+
result.push(node)
25+
}
26+
return left || right || middle
27+
}
28+
helper(root)
29+
return result[0];
30+
};

solutions/240_Searcha2DMatrixII.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {number[][]} matrix
3+
* @param {number} target
4+
* @return {boolean}
5+
*/
6+
var searchMatrix = function(matrix, target) {
7+
function search(x1, y1, x2, y2){
8+
if(x2 < x1 || y2 < y1 || x1 >= matrix.length || y1 >= matrix[0].length || y2 < 0 || x2 < 0){
9+
return false;
10+
}
11+
let middle_x = Math.floor((x2-x1)/2) + x1;
12+
let middle_y = Math.floor((y2-y1)/2) + y1;
13+
let current = matrix[middle_x][middle_y];
14+
if(current == target){
15+
return true;
16+
} else if(current < target){
17+
//search in: whole down (+1 from middle WHY?) and right top (+1 from middle WHY?)
18+
return search(x1, middle_y+1, x2, y2) || // whole down
19+
search(middle_x+1, y1, x2, middle_y) //
20+
} else if (current > target){
21+
//search in: whole up (+1 from middle WHY?) and left down (-1 from middle WHY?)
22+
return search(x1, y1, x2, middle_y-1) || // whole up
23+
search(x1, middle_y, middle_x-1,y2)
24+
} else {
25+
return false;
26+
}
27+
}
28+
if(matrix.length === 0 || matrix[0].length === 0){
29+
return false
30+
}
31+
return search(0, 0 , matrix.length-1, matrix[0].length-1)
32+
};

solutions/240_Searcha2DMatrixII.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution(object):
2+
def searchMatrix(self, matrix, target):
3+
"""
4+
:type matrix: List[List[int]]
5+
:type target: int
6+
:rtype: bool
7+
"""
8+
if len(matrix) == 0 or len(matrix[0])==0:
9+
return False
10+
return self.search(0,0,len(matrix)-1, len(matrix[0])-1, matrix, target)
11+
12+
def search(self, x1, y1, x2, y2, matrix, target):
13+
if x2 < x1 or y2 < y1 or x1 >= len(matrix) or y1 >= len(matrix[0]) or x2 < 0 or y2 < 0:
14+
return False
15+
16+
middle_x = x1 + (x2-x1) // 2
17+
middle_y = y1 + (y2-y1) // 2
18+
19+
current = matrix[middle_x][middle_y]
20+
21+
if current == target:
22+
return True
23+
elif current < target:
24+
return (self.search(x1, middle_y+1, x2, y2, matrix, target) or
25+
self.search(middle_x+1, y1, x2, middle_y, matrix, target))
26+
elif current > target:
27+
return (self.search(x1, y1, x2, middle_y-1, matrix, target) or
28+
self.search(x1, middle_y, middle_x-1, y2, matrix, target))
29+
else:
30+
return False

solutions/242_ValidAnagram.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {boolean}
5+
*/
6+
var isAnagram = function(s, t) {
7+
if(s.length != t.length){
8+
return false
9+
}
10+
let dict = {};
11+
for(let i = 0 ;i<s.length;i++){
12+
if(dict[s[i]]){
13+
dict[s[i]]++
14+
} else {
15+
dict[s[i]] = 1
16+
}
17+
}
18+
for(let j = 0; j<t.length;j++){
19+
if(dict[t[j]]){
20+
dict[t[j]]--;
21+
if(dict[t[j]] <= 0){
22+
delete dict[t[j]]
23+
}
24+
} else {
25+
return false
26+
}
27+
}
28+
return true
29+
};

solutions/257_BinaryTreePath.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {string[]}
11+
*/
12+
var binaryTreePaths = function(root) {
13+
let result = [];
14+
if(!root){
15+
return []
16+
}
17+
function helper(root, current){
18+
if(!root.left && !root.right){
19+
result.push(current + root.val);
20+
return;
21+
}
22+
if(root.left){
23+
helper(root.left,current+root.val+'->')
24+
}
25+
if(root.right){
26+
helper(root.right,current+root.val+'->')
27+
}
28+
29+
}
30+
helper(root,'');
31+
return result
32+
};

solutions/283_MoveZeros.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {void} Do not return anything, modify nums in-place instead.
4+
*/
5+
var moveZeroes = function(nums) {
6+
let j = 0
7+
for (let i = 0; i<nums.length;i++){
8+
if(nums[i]!==0){
9+
let tmp = nums[i]
10+
nums[i] = nums[j]
11+
nums[j] = tmp
12+
j++
13+
}
14+
}
15+
};
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var findDuplicate = function(nums) {
6+
let dict = {}
7+
for(let i = 0; i<nums.length;i++){
8+
if(dict[nums[i]]){
9+
return nums[i]
10+
} else {
11+
dict[nums[i]] = true
12+
}
13+
}
14+
return -1
15+
};

solutions/289_GameofLife.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @param {number[][]} board
3+
* @return {void} Do not return anything, modify board in-place instead.
4+
*/
5+
var gameOfLife = function(board) {
6+
let rows = board.length;
7+
let cols = board[0].length;
8+
function initMatrix(rows,cols){
9+
let newMatrix = [];
10+
for(let i=0;i<rows;i++){
11+
newMatrix[i] = []
12+
}
13+
return newMatrix;
14+
}
15+
function getCurrentState(board,curr_i,curr_j){
16+
let start_i = curr_i-1 > 0 ? curr_i-1 : 0;
17+
let start_j = curr_j-1 > 0 ? curr_j-1:0;
18+
let end_i = curr_i+1 < board.length-1? curr_i+1 : board.length-1;
19+
let end_j = curr_j+1 < board[0].length-1? curr_j+1 : board[0].length-1;
20+
let neighbors_cnt = 0;
21+
for(let i = start_i; i<=end_i;i++){
22+
for(let j = start_j;j<=end_j;j++){
23+
if(board[i][j]==1){
24+
neighbors_cnt++
25+
}
26+
}
27+
}
28+
let next_element = undefined;
29+
let current_element = board[curr_i][curr_j]
30+
if(current_element == 1){
31+
//because we can count ourself
32+
neighbors_cnt = neighbors_cnt -1;
33+
next_element = (neighbors_cnt > 3 || neighbors_cnt < 2) ? 0 : 1;
34+
} else {
35+
next_element = neighbors_cnt === 3 ? 1: current_element;
36+
}
37+
return next_element;
38+
}
39+
40+
let new_matrix = initMatrix(rows,cols);
41+
for(let i = 0;i<rows;i++){
42+
for(let j =0;j<cols;j++){
43+
new_matrix[i][j] = getCurrentState(board,i,j);
44+
}
45+
}
46+
47+
//copying new_matrix to board
48+
for(let i = 0;i<rows;i++){
49+
for(let j = 0;j<cols;j++){
50+
board[i][j] = new_matrix[i][j]
51+
}
52+
}
53+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
9+
/**
10+
* Encodes a tree to a single string.
11+
*
12+
* @param {TreeNode} root
13+
* @return {string}
14+
*/
15+
var serialize = function(root) {
16+
let result = []
17+
function helper(root){
18+
if(!root){
19+
result.push("#")
20+
} else {
21+
result.push(root.val);
22+
helper(root.left)
23+
helper(root.right)
24+
}
25+
}
26+
helper(root)
27+
return result.toString();
28+
};
29+
30+
/**
31+
* Decodes your encoded data to tree.
32+
*
33+
* @param {string} data
34+
* @return {TreeNode}
35+
*/
36+
var deserialize = function(data) {
37+
let arr = data.split(',');
38+
var index = 0;
39+
function helper(arr){
40+
if(arr[index] == '#' || index > arr.length){
41+
return null;
42+
}
43+
let root = new TreeNode(parseInt(arr[index]));
44+
index = index + 1;
45+
root.left = helper(arr);
46+
index = index + 1;
47+
root.right = helper(arr)
48+
return root
49+
}
50+
return helper(arr)
51+
};
52+
53+
/**
54+
* Your functions will be called as such:
55+
* deserialize(serialize(root));
56+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var lengthOfLIS = function(nums) {
6+
let dp = new Array(nums.length).fill(0);
7+
dp[0] = 1;
8+
let result = 1;
9+
if(nums.length == 0){
10+
return 0
11+
}
12+
// in dp contains all subsequence length;
13+
// [10,9,2,5,3,7,101,18]
14+
// [1,0,0,0,0,0,0,0] // step 1
15+
// [1,1,0,0,0,0,0,0] // step 2. 9 < 10 so numbers are not growing.
16+
// [1,1,1,0,0,0,0,0] // step 3. 2 < 10 || 9 so numbers are not growing.
17+
// [1,1,1,2,0,0,0,0] // step 4. 5 > 2 -> dp[indexOf(2) == 2] == 1; dp[i] = 1 + 1(beucase 1 more element in sequense)
18+
// [1,1,1,2,2,0,0,0] // step 5. 3 > 2 -> dp[indexOf(2) == 2] == 1; dp[i] = 1 + 1
19+
// [1,1,1,2,2,3,0,0] // step 6. 7 > 3 -> dp[indexOf(3) == 4] == 2; dp[i] = 2 + 1;
20+
// [1,1,1,2,2,3,4,0] // step 7. 101 > 7 -> dp[indexOf(7) == 5] == 3; dp[i] = 3 + 1;
21+
// [1,1,1,2,2,3,4,4] // step 8. 10 > 7 -> dp[indexOf(7) == 5] == 3; dp[i] = 3+1;
22+
// If we have array like [10,9,2,5,3,7,101,102]; then
23+
// [1,1,1,2,2,3,4,4,5] // step 9 102 > 101; dp[indexOf(101) == 6] == 4; dp[i] = 4 +1;
24+
for(let i = 1;i<nums.length;i++){
25+
let max_length = 0
26+
for(let j = 0; j<i;j++){
27+
if(nums[i] > nums[j]){
28+
max_length = Math.max(max_length, dp[j])
29+
}
30+
}
31+
dp[i] = max_length + 1;
32+
result = Math.max(dp[i], result);
33+
}
34+
return result
35+
};

0 commit comments

Comments
 (0)