Movatterモバイル変換


[0]ホーム

URL:


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

Added another solution to Permutation ||#4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
chihungyu1116 merged 11 commits intochihungyu1116:masterfromignacio-chiazzo:master
Feb 27, 2017
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
11 commits
Select commitHold shift + click to select a range
efdff0e
Added another solution to Permutation ||
ignacio-chiazzoFeb 18, 2017
47a816e
third solution subset
ignacio-chiazzoFeb 22, 2017
bd0c05b
Merge pull request #1 from ignacio-chiazzo/subsets
ignacio-chiazzoFeb 22, 2017
a05d4ec
combination Sum DFS Solution
ignacio-chiazzoFeb 22, 2017
2c45fdd
Merge pull request #2 from ignacio-chiazzo/combination-sum
ignacio-chiazzoFeb 22, 2017
265a17f
another shorter solution to ind minimun in rotated array problem
ignacio-chiazzoFeb 23, 2017
f2425f7
Merge pull request #3 from ignacio-chiazzo/find-minimum-in-rotated-array
ignacio-chiazzoFeb 23, 2017
f0235cc
clearer solution rotate image
ignacio-chiazzoFeb 24, 2017
6d34381
Merge pull request #4 from ignacio-chiazzo/rotate-image
ignacio-chiazzoFeb 24, 2017
ff610c9
another recursive solution
ignacio-chiazzoFeb 27, 2017
13b5dbd
Merge pull request #5 from ignacio-chiazzo/btToLL
ignacio-chiazzoFeb 27, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions114 Flatten Binary Tree to Linked List.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,21 +16,44 @@
var flatten = function(root) {
var stack = [];
var p = root;

while(p !== null || stack.length !== 0){
if(p.right !== null){
stack.push(p.right);
}

if(p.left !== null){ // [!!!]point of confusing, if null then pop stack
p.right = p.left;
p.left = null;
} else if(stack.length !== 0){
var node = stack.pop();
p.right = node;
}

p = p.right;
}
};

// Recursive solution

var flatten = function(root) {
if(root === null || (root.left === null && root.right === null)) {
return;
}

var rootLeft = root.left;
var rootRight = root.right;
root.left = null;
root.right = null;

flatten(rootLeft);
flatten(rootRight);

root.right = rootLeft;

var aux = root;
while(aux !== null && aux.right !== null) {
aux = aux.right;
}
aux.right = rootRight;
};
19 changes: 14 additions & 5 deletions153 Find Minimum in Rotated Sorted Array.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,19 +10,28 @@ var findMin = function(nums) {
vars=0;
vare=nums.length-1;
varmin;

while(s<e-1){
varmid=s+parseInt((e-s)/2);

if(nums[mid]<nums[s]){
e=mid;
}elseif(nums[mid]>nums[e]){
s=mid;
}else{
returnnums[s];
}

}

returnMath.min(nums[e],nums[s]);
};
};

// Another shorter solution;
varfindMin=function(nums){
vari=0;
while(i<nums.length-1&&nums[i]<nums[i+1]){
i++;
}
return(i===nums.length-1)?nums[0] :nums[i+1]
};
46 changes: 35 additions & 11 deletions39 Combination Sum.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,8 +5,8 @@
// Note:
// All numbers (including target) will be positive integers.
// The solution set must not contain duplicate combinations.
// For example, given candidate set [2, 3, 6, 7] and target 7,
// A solution set is:
// For example, given candidate set [2, 3, 6, 7] and target 7,
// A solution set is:
// [
// [7],
// [2, 2, 3]
Expand All@@ -24,37 +24,61 @@
*/
var combinationSum = function(candidates, target) {
var result = [];

if(candidates === null || candidates.length === 0){
return result;
}

candidates.sort(function(a,b){return a > b ? 1 : -1});

var output = [];

generate(candidates, result, output, target, 0);

return result;
};

var generate = function(candidates, result, output, sum, index){
if(sum === 0){
result.push(output.slice());
result.push(output.slice());
}
if(sum < 0){
return;
}

for(var i = index; i < candidates.length; i++){
if(i > index && candidates[i] === candidates[i - 1]){
continue;
}

if(candidates[i] <= sum){
output.push(candidates[i]);
generate(candidates, result, output, sum - candidates[i], i);
output.pop();
}
}
}
}


// Another solution
var combinationSum = function(candidates, target) {
var results = [];
comb(candidates.sort(), 0, [], 0, target, results);
return results;
};

var comb = function(cand, index, partial, partialSum, target, results) {
if(target === partialSum) {
results.push(partial);
return;
}
if(cand.length === index || partialSum > target) {
return;
}

comb(cand, index + 1, partial, partialSum, target, results);
comb(cand, index, partial.concat([cand[index]].concat([cand[index]])),
partialSum + 2* cand[index], target, results);
comb(cand, index + 1, partial.concat([cand[index]]),
partialSum + cand[index], target, results);
};
30 changes: 29 additions & 1 deletion47 Permutations II.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -50,4 +50,32 @@ var generatePermute = function(nums, step, currentResult, visited, finalResult)
visited[i]=false;
}
}
}
}

//Another Solution, similar approach that Permutation.js
varpermuteUnique=function(nums){
returnpermut(nums.sort(),[]);
};

varpermut=function(nums,partial){
if(nums.length===0){
return[partial];
}
varlistSol=[];
for(vari=0;i<nums.length;i++){
varendRepeated=i;
while(endRepeated<nums.length&&nums[i]===nums[endRepeated]){
endRepeated++;
}

vararrayWithoutI=nums.slice(0,i).concat(nums.slice(i+1,nums.length));
varpartialSol=partial.concat([nums[i]]);
varsol=permut(arrayWithoutI,partialSol);
if(sol.length>0){
listSol=listSol.concat(sol);
}
i=endRepeated-1;
}
returnlistSol;
};

43 changes: 38 additions & 5 deletions48 Rotate Image.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,20 +11,20 @@
*/
var rotate = function(matrix) {
var row = matrix.length;

if(row === 0) {
return;
}

var col = matrix[0].length;

// swap them in diagonal
for(var i = 0; i < row; i++) {
for(var j = 0; j < col - i; j++) {
swap(matrix, i, j, row - 1 - j, col - 1 - i);
}
}

// swap in middle
for(i = 0; i < Math.floor(row/2); i++) {
for(j = 0; j < col; j++) {
Expand All@@ -37,4 +37,37 @@ function swap(matrix, x1, y1, x2, y2) {
var tmp = matrix[x1][y1];
matrix[x1][y1] = matrix[x2][y2];
matrix[x2][y2] = tmp;
}
}

//Clearer Solution
var rotate = function(matrix) {
rotateColumns(matrix);
rotateEachDiagonal(matrix);
};

var rotateColumns = function(matrix) {
for(var j = 0; j < matrix.length; j++) {
var low = 0;
var ceil = matrix.length -1;
while(low < ceil) {
swap(matrix, low, j, ceil, j);
low++;
ceil--;
}
}
};

var rotateEachDiagonal = function(matrix){
for(var i = 0; i < matrix.length; i++) {
for(var j = i; j < matrix.length; j++) {
swap(matrix, i, j, j, i);
}
}
};

var swap = function(matrix, i1, j1, i2, j2) {
var aux = matrix[i1][j1];
matrix[i1][j1] = matrix[i2][j2];
matrix[i2][j2] = aux;
};

27 changes: 21 additions & 6 deletions78 Subsets.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -32,7 +32,7 @@ var generate = function(nums, index, cur, result) {
result.push(cur.slice());
return
}

generate(nums, index + 1, cur, result);
cur.push(nums[index]);
generate(nums, index + 1, cur, result);
Expand All@@ -42,11 +42,9 @@ var generate = function(nums, index, cur, result) {


// second try


var subsets = function(nums) {
var res = [[]];

function generate(nums, i, cur, res) {
for(; i < nums.length; i++) {
cur.push(nums[i]);
Expand All@@ -55,7 +53,24 @@ var subsets = function(nums) {
cur.pop();
}
}

generate(nums, 0, [], res);
return res;
};
};

// Third solution
var subsets = function(nums) {
var results = [];
combine(nums, 0, [], results);
return results;
}

var combine = function(nums, index, partial, results) {
if(index === nums.length) {
results.push(partial);
return;
}

combine(nums, index + 1, partial, results);
combine(nums, index + 1, partial.concat([nums[index]]), results);
}

[8]ページ先頭

©2009-2025 Movatter.jp