Skip to content

Commit

Permalink
Thu, Jul 28, 2016 11:21:03 AM
Browse files Browse the repository at this point in the history
  • Loading branch information
Manish Jain committed Jul 28, 2016
1 parent 4d93e36 commit 3540216
Show file tree
Hide file tree
Showing 271 changed files with 15,907 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Big-Data-Module/Data-Structure-Javascript/index/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ console.log("areParenthesisBalanced('{()}[]') ",areParenthesisBalanced('{()}[]'

var binaryIndexOf = function(items,searchElement){
items = items.sort(function(a,b){return a - b;});
var minIndex = 0,maxIndex = items.length - 1,currentIndex = 0,currentElement = null;
var minIndex = 0,maxIndex = items.length,currentIndex = 0,currentElement = null;
while(minIndex <= maxIndex){
currentIndex = Math.floor((minIndex + maxIndex)/2);
currentElement = items[currentElement];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var binaryIndexOf = function(items,searchElement){
var minIndex = 0,maxIndex = items.length,currentIndex,currentElelment;
while(maxIndex >= minIndex){
currentIndex = Math.floor((minIndex + maxIndex)/2);
currentElelment = items[currentIndex];
if(currentElelment < searchElement){
minIndex = currentIndex + 1;
}else if(currentElelment > searchElement){
maxIndex = currentIndex - 1;
}else{
return currentIndex;
}
}
return null
}

console.log('binaryIndexOf(3) ',binaryIndexOf([2,7,5,1,34,3],3))
49 changes: 49 additions & 0 deletions leetcode-js-master/3Sum Closest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
For example, given array S = {-1 2 1 -4}, and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
*/
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var threeSumClosest = function(nums, target) {
var len = nums.length,
minDiff = Number.MAX_VALUE,
diff,
left,
right,
i,
j;

nums.sort(function(a, b) {
return a - b;
});

for (i = 0; i < len; i++) {
left = i + 1;
right = len - 1;

while (left < right) {
diff = target - nums[i] - nums[left] - nums[right];

if (diff === 0) {
return target;
} else if (diff > 0) {
left++;
} else {
right--;
}

if (Math.abs(diff) < Math.abs(minDiff)) {
minDiff = diff;
}
}
}

return target - minDiff;
};
73 changes: 73 additions & 0 deletions leetcode-js-master/3Sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
The solution set must not contain duplicate triplets.
For example, given array S = {-1 0 1 2 -1 -4},
A solution set is:
(-1, 0, 1)
(-1, -1, 2)
*/
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function(nums) {
nums.sort(function(a, b) {
return a - b;
});

var len = nums.length,
i,
result = [],
curSol = [];

for (i = 0; i < len; i++) {
if (i > 0 && nums[i] === nums[i - 1]) {
continue;
}

curSol.push(nums[i]);
twoSum(result, curSol.concat(), i + 1, len - 1, -nums[i], nums);
curSol.pop();
}

return result;
};

function twoSum(allSol, curSol, startIndex, endIndex, target, nums) {
var start = startIndex,
end = endIndex,
sum,
mid;

while (start < end) {
sum = nums[start] + nums[end];

if (sum === target) {
curSol.push(nums[start]);
curSol.push(nums[end]);
allSol.push(curSol.concat());
curSol.pop();
curSol.pop();

start++;
end--;

while (nums[start] === nums[start - 1]) {
start++;
}

while (nums[end] === nums[end + 1]) {
end--;
}
} else if (sum < target) {
start++;
} else {
end--;
}
}
}
91 changes: 91 additions & 0 deletions leetcode-js-master/4Sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note:
Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
The solution set must not contain duplicate quadruplets.
For example, given array S = {1 0 -1 0 -2 2}, and target = 0.
A solution set is:
(-1, 0, 0, 1)
(-2, -1, 1, 2)
(-2, 0, 0, 2)
*/
/**
* @param {number[]} nums
* @param {number} target
* @return {number[][]}
*/
var fourSum = function(nums, target) {
nums.sort(function(a, b) {
return a - b;
});

var len = nums.length,
result = [],
i;

kSum(result, [], 4, nums, target, 0, len - 1);

return result;
};

function kSum(result, curArr, k, nums, target, startIndex, endIndex) {
var len = nums.length,
start,
end,
sum,
i;

if (k >= 3) {
for (i = startIndex; i <= endIndex; i++) {
if (i > startIndex && nums[i] === nums[i - 1]) {
continue;
}

curArr.push(nums[i]);
kSum(result, curArr.concat(), k - 1, nums, target - nums[i], i + 1, endIndex);
curArr.pop();
}
}

if (k === 1) {
for (i = startIndex; i <= endIndex; i++) {
if (nums[i] === target) {
result.push(nums[i]);
}
}
}

if (k === 2) {
start = startIndex;
end = endIndex;

while (start < end) {
sum = nums[start] + nums[end];

if (sum === target) {
curArr.push(nums[start]);
curArr.push(nums[end]);
result.push(curArr.concat());
curArr.pop();
curArr.pop();

start++;
end--;

while(nums[start] === nums[start - 1]) {
start++;
}

while(nums[end] === nums[end + 1]) {
end--;
}
} else if (sum < target) {
start++;
} else {
end--;
}
}
}
}
78 changes: 78 additions & 0 deletions leetcode-js-master/Add Binary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
*/

/**
* @param {string} a
* @param {string} b
* @return {string}
*/
var addBinary = function(a, b) {
var lenA = a.length,
lenB = b.length,
overFlow = 0,
charA,
charB,
result = '',
curVal,
i,
j;

for (i = lenA - 1, j = lenB - 1; i >= 0 && j >= 0; i--, j --) {
charA = parseInt(a.charAt(i));
charB = parseInt(b.charAt(j));

curVal = charA + charB + overFlow;

if (curVal > 1) {
curVal = curVal - 2;
overFlow = 1;
} else {
overFlow = 0;
}

result = curVal + result;
}

while (i >= 0) {
charA = parseInt(a.charAt(i));
curVal = charA + overFlow;

if (curVal > 1) {
curVal = curVal - 2;
overFlow = 1;
} else {
overFlow = 0;
}
result = curVal + result;

i--;
}

while (j >= 0) {
charB = parseInt(b.charAt(j));
curVal = charB + overFlow;

if (curVal > 1) {
curVal = curVal - 2;
overFlow = 1;
} else {
overFlow = 0;
}

result = curVal + result;

j--;
}

if (overFlow === 1) {
result = '1' + result;
}

return result;
};
25 changes: 25 additions & 0 deletions leetcode-js-master/Add Digits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
For example:
Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
Hint:
A naive implementation of the above process is trivial. Could you come up with other methods?
What are all the possible results?
How do they occur, periodically or randomly?
You may find this Wikipedia article useful.
*/

/**
* @param {number} num
* @return {number}
*/
var addDigits = function(num) {
return (num - 1) % 9 + 1;
};
Loading

0 comments on commit 3540216

Please sign in to comment.