Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
Lines changed: 26 additions & 0 deletions
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+
};
Lines changed: 30 additions & 0 deletions
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‎

Lines changed: 32 additions & 0 deletions
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‎

Lines changed: 30 additions & 0 deletions
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‎

Lines changed: 29 additions & 0 deletions
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‎

Lines changed: 32 additions & 0 deletions
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‎

Lines changed: 15 additions & 0 deletions
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+
};
Lines changed: 15 additions & 0 deletions
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‎

Lines changed: 53 additions & 0 deletions
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+
};
Lines changed: 56 additions & 0 deletions
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+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /