Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions LeetCode/javascript/missing_number/missingnumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var missingNumber = function(nums) {
let sum = 0, total = 0
for(let i = 0; i < nums.length; i++) {
sum += nums[i]
total += i + 1
}
return total - sum
};
20 changes: 20 additions & 0 deletions LeetCode/javascript/missing_number/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Missing Number
# Easy

Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.

Example 1:

Input: nums = [3,0,1]
Output: 2
Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.
Example 2:

Input: nums = [0,1]
Output: 2
Explanation: n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums.
Example 3:

Input: nums = [9,6,4,2,3,5,7,0,1]
Output: 8
Explanation: n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums.
19 changes: 19 additions & 0 deletions LeetCode/javascript/product_of_array_except_self/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## Product of Array Except Self
# Medium

Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].

The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.

You must write an algorithm that runs in O(n) time and without using the division operation.



Example 1:

Input: nums = [1,2,3,4]
Output: [24,12,8,6]
Example 2:

Input: nums = [-1,1,0,-3,3]
Output: [0,0,9,0,0]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function productExceptSelf(nums) {
const result = [];
let currentProduct = 1;
for (let i = 0; i < nums.length; i++) {
result[i] = currentProduct
currentProduct *= nums[i]
}
currentProduct = 1
for (let j = nums.length-1; j >= 0; j--) {
result[j] *= currentProduct
currentProduct *= nums[j]
}
return result;
};
23 changes: 23 additions & 0 deletions LeetCode/javascript/two_sum/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## Two Sum
# Easy
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

Example 2:

Input: nums = [3,2,4], target = 6
Output: [1,2]

Example 3:

Input: nums = [3,3], target = 6
Output: [0,1]
9 changes: 9 additions & 0 deletions LeetCode/javascript/two_sum/twosum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const twoSum = function (nums, target) {
for (let i = 0; i < nums.length; i++) {
for (let j = i + 1; i < nums.length; j++) {
if(nums[i] + nums[j] === target) {
return [i, j]
}
}
}
}