Giter Site home page Giter Site logo

vjechsmayr / javascriptalgorithms Goto Github PK

View Code? Open in Web Editor NEW
57.0 4.0 92.0 412 KB

All Algorithms implemented in JavaScript - a project for hacktoberfest2019 - NO new Issues/PRs for hacktoberfest 2020 or 2021 - thank you

JavaScript 100.00%
javascript algorithm hacktoberfest2019

javascriptalgorithms's Introduction

The Algorithms - JavaScript

All algorithms implemented in JavaScript (for education)

There implementations are for learning purposes. If you want to contribute more efficient solutions feel free to open an issue and commit your solution.

This Repository is open for Hacktoberfest 2019 ONLY! No PRs/Issues for Hacktoberfest 2020 or later will be accepted and marked as invalid/spam

Inspiration

You can look for Algorithms to implement at: LeetCode Algorithms

To contribute make sure the Algorithm isn't commited yet! Make sure to add the number of the Issue in your PR.

Folders and Files

Please make sure your file is in the correct Folder - You can find a Algorithms_ToDo-File in the Folders where your Algorithm is listed. Please delete your Algorithm from this file when adding your file with the Algorithm to the directory.

Getting Started

  • Fork this repository (Click the Form button, top right of this page)
  • Clone your fork down to your local machine
git clone https://github.com/your-username/hacktoberfest.git
  • Create a branch for a new feature
git checkout -b feature/branch-name
  • Or if its a bugfix to a file
git checkout -b bugfix/branch-name
  • Make your changes (choose from the Tasks above!)
  • Commit and Push
git add .
git commit -m 'commit message'
git push origin branch-name
  • Create a New Pull Request from your forked repository ( Click the 'New Pull Request' Button located at the top of your repo)
  • Wait for your PR review and merge approval!
  • Star this repository if you had fun!

Thank You!

javascriptalgorithms's People

Contributors

aprilxyc avatar ccjmne avatar cheec avatar danielcaldas avatar dependabot[bot] avatar dhkamp avatar iron-bergh avatar j-purview avatar josehkburger avatar jrga03 avatar lanhbao avatar lashuk1729 avatar marialuize avatar nathanpickard avatar osusara avatar paulatulis avatar paulooosrj avatar pawankolhe avatar pragunsaini avatar priyanshu2510 avatar psalmfill avatar qdtroemner avatar quadriphobs1 avatar raffleberry avatar rajneeshkumar146 avatar rubiin avatar sarahsakhri avatar theleek avatar uddeshjain avatar vjechsmayr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

javascriptalgorithms's Issues

Tree Sort

looking for the "Tree Sort"-Algorithm in JavaScript

Reshape the Matrix

See LeetCode Problem here

Insert file 566_ReshapeTheMatrix.js into folder LeetCode Solutions

In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data.

You're given a matrix represented by a two-dimensional array, and two positive integers r and c representing the row number and column number of the wanted reshaped matrix, respectively.

The reshaped matrix need to be filled with all the elements of the original matrix in the same row-traversing order as they were.

If the 'reshape' operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.

Example 1:

Input: 
nums = 
[[1,2],
 [3,4]]
r = 1, c = 4
Output: 
[[1,2,3,4]]
Explanation:
The row-traversing of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list.

Note:

  • The height and width of the given matrix is in range [1, 100].
  • The given r and c are all positive.

Code

/**
 * @param {number[][]} nums
 * @param {number} r
 * @param {number} c
 * @return {number[][]}
 */
var matrixReshape = function(nums, r, c) {
    
};

Subsets

See LeetCode Problem here

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: nums = [1,2,3]
Output:
[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

Code

/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var subsets = function(nums) {
    
};

Bubble Sort

looking for the "Bubble Sort"-Algorithm in JavaScript

Permutations Algorithm

See LeetCode Problem here

Given a collection of distinct integers, return all possible permutations.

Example:

Input: [1,2,3]
Output:
[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]

Code:

/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var permute = function(nums) {
    
};

Fibonacci Search Algorithm

Given a sorted array arr[] of size n and an element x to be searched in it. Return index of x if it is present in array else return -1.
Examples:

Input:  arr[] = {2, 3, 4, 10, 40}, x = 10
Output:  3
Element x is present at index 3.

Input:  arr[] = {2, 3, 4, 10, 40}, x = 11
Output:  -1
Element x is not present.

Fibonacci Search is a comparison-based technique that uses Fibonacci numbers to search an element in a sorted array.

Generate Parentheses

See LeetCode Problem here

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

For example, given n = 3, a solution set is:

[
  "((()))",
  "(()())",
  "(())()",
  "()(())",
  "()()()"
]

Code
/**
@param {number} n
@return {string[]}
*/
var generateParenthesis = function(n) {

};

Valid Sudoku Algorithm

Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

  1. Each row must contain the digits 1-9 without repetition.
  2. Each column must contain the digits 1-9 without repetition.
  3. Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.

The Sudoku board could be partially filled, where empty cells are filled with the character '.'.

Input:
[
  ["5","3",".",".","7",".",".",".","."],
  ["6",".",".","1","9","5",".",".","."],
  [".","9","8",".",".",".",".","6","."],
  ["8",".",".",".","6",".",".",".","3"],
  ["4",".",".","8",".","3",".",".","1"],
  ["7",".",".",".","2",".",".",".","6"],
  [".","6",".",".",".",".","2","8","."],
  [".",".",".","4","1","9",".",".","5"],
  [".",".",".",".","8",".",".","7","9"]
]
Output: true

Note:

  • A Sudoku board (partially filled) could be valid but is not necessarily solvable.
  • Only the filled cells need to be validated according to the mentioned rules.
  • The given board contain only digits 1-9 and the character '.'.
  • The given board size is always 9x9.

See LeetCode Problem here: Valid Sudoku

Two Sum

See Problem here: Two Sum

Add your Solution to the Other Algorithm Directory

Regular Expression Matching

See LeetCode Problem here: Regular Expression Matching

Create your file in the folder Backtracking - don't forget to delete Regular Expression Matching from the AlgorithmsToDo-file

Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'.

'.' Matches any single character.
'*' Matches zero or more of the preceding element.

The matching should cover the entire input string (not partial).

Note:

  • s could be empty and contains only lowercase letters a-z.
  • p could be empty and contains only lowercase letters a-z, and characters like . or *.

Code:

/**
 * @param {string} s
 * @param {string} p
 * @return {boolean}
 */
var isMatch = function(s, p) {
    
};

Roman to (decimal) Number

Algorithm to Convert a Roman Number to a decimal Number

Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

Example:

Input: "IV"
Output: 4

See Problem here

Letter Combinations of a Phone Number

See LeetCode Problem here: Letter Combinations

Create your file in the folder Backtracking - don't forget to delete Letter Combinations of a Phone Number from the AlgorithmsToDo`-file

Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.

A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.

Example:

Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.

Code:

/**
 * @param {string} digits
 * @return {string[]}
 */
var letterCombinations = function(digits) {
    
};

Setup/Organisation Structure

It would be a good idea to have few things in place for a good organisation and to support for a contribution

  1. Contribution guide
  2. License
  3. Use of JavaScript/Node package.json to manage commands
  4. Writing extensive for each algorithm to verify its validity
  5. Folder organisation for each algorithm in its own scope

Suggetion for folder

package.json
insertionsort/
insertionsort/insertionsort.js
insertionsort/insertionsort.test.js

I can create a quick PR for the folder organisation if that will be welcomed

Fibonacci Algorithm

Algorithm to calculate the Fibonacci Series.

Input: 10
Output: 1 1 2 3 5 8 13 21 34 55

Combination Sum

Create File combinationSum.js into directory Backtracking

See LeetCodeProblem here

Create your file in the folder Backtracking - don't forget to delete Combination Sum from the AlgorithmsToDo`-file

Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.

The same repeated number may be chosen from candidates unlimited number of times.

Note:

  • All numbers (including target) will be positive integers.
  • The solution set must not contain duplicate combinations.

Example 1:

Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
  [7],
  [2,2,3]
]

Solutions for LeetCode in JavaScript

You can contribute solutions for LeetCode algorithm with the programming language JavaScript

Look in the folders for the Algorithms_ToDo-file to see which Problems are open to contribute.

Good practice coding :)

Permutations 2 Algorithm

See LeetCode Problem here

Given a collection of numbers that might contain duplicates, return all possible unique permutations.

Example:

Input: [1,1,2]
Output:
[
  [1,1,2],
  [1,2,1],
  [2,1,1]
]

Code

/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var permuteUnique = function(nums) {
    
};

Restore IP-Addresses

Create file restore_ipAddresses.js it folder Backtracking - don't forget to delete Combination Sum from the AlgorithmsToDo`-file

See LeetCode Problem here

Given a string containing only digits, restore it by returning all possible valid IP address combinations.

Example:

Input: "25525511135"
Output: ["255.255.11.135", "255.255.111.35"]

Permutation Algorithm

Given a collection of distinct integers, return all possible permutations.

Input: [1,2,3]
Output:
[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]

See Problem here

N-Queens

Create file nqueens.js at folder Backtracking - don't forget to delete Combination Sum from the AlgorithmsToDo`-file

See LeetCode Problem here

The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.

Given an integer n, return all distinct solutions to the n-queens puzzle.

Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively.

Example:

Input: 4
Output: [
 [".Q..",  // Solution 1
  "...Q",
  "Q...",
  "..Q."],

 ["..Q.",  // Solution 2
  "Q...",
  "...Q",
  ".Q.."]
]

Binary Addition

Given two binary strings, return their sum (also a binary string).

The input strings are both non-empty and contains only characters 1 or 0.

Input: a = "11", b = "1"
Output: "100"

Code:

/**
 * @param {string} a
 * @param {string} b
 * @return {string}
 */
var addBinary = function(a, b) {
    
};

See Problem here

Palindrome Number Algorithm

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Input: 121
Output: true

See Problem here

Converted Sorted List to Binary Search Tree

Algorithm for Problem 109 at LeetCode.com

Example:

Given the sorted linked list: [-10,-3,0,5,9],

One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:

      0
     / \
   -3   9
   /   /
 -10  5

Code:

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {TreeNode}
 */
var sortedListToBST = function(head) {
    
};

See Problem here

Day of the Week

See LeetCode Problem here

Insert file 1185_DayOfTheWeek.js into folder LeetCode Solutions

Given a date, return the corresponding day of the week for that date.

The input is given as three integers representing the day, month and year respectively.

Return the answer as one of the following values {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}.

Example 1:

Input: day = 31, month = 8, year = 2019
Output: "Saturday"

Example 2:

Input: day = 18, month = 7, year = 1999
Output: "Sunday"

Example 3:

Input: day = 15, month = 8, year = 1993
Output: "Sunday"

Constraints:

  • The given dates are valid dates between the years 1971 and 2100.

Wildcard Matching Algorithm

See Problem here

Create your file in the folder Backtracking - don't forget to delete Wildcard Matching from the AlgorithmsToDo`-file

Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'.

'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).

The matching should cover the entire input string (not partial).

Note:

s could be empty and contains only lowercase letters a-z.
p could be empty and contains only lowercase letters a-z, and characters like ? or *.

Example:

Input:
s = "aa"
p = "*"
Output: true
Explanation: '*' matches any sequence.

Permission to add WeightedMean Algorithm

Hello, I was wondering if I could add an algorithm to find the weighted mean of a list of numbers and their weights?

I've already completed the algorithm, I just wanted to get permission to add it before I opened a pull request.

Sudoku Solver

Write an Algorithm to Solve a Sudoku puzzle by filling the empty cells.

Empty cells are indicated by the character '.'.

Input:
[
  ["5","3",".",".","7",".",".",".","."],
  ["6",".",".","1","9","5",".",".","."],
  [".","9","8",".",".",".",".","6","."],
  ["8",".",".",".","6",".",".",".","3"],
  ["4",".",".","8",".","3",".",".","1"],
  ["7",".",".",".","2",".",".",".","6"],
  [".","6",".",".",".",".","2","8","."],
  [".",".",".","4","1","9",".",".","5"],
  [".",".",".",".","8",".",".","7","9"]
]

See Problem here

Insertion Sort

looking for the "Insertion Sort"-Algorithm in JavaScript

Combinations

See LeetCode Problem here

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.

Example:

Input: n = 4, k = 2
Output:
[
  [2,4],
  [3,4],
  [2,3],
  [1,2],
  [1,3],
  [1,4],
]

Merge Sort

looking for the "Merge Sort"-Algorithm in JavaScript

Selection Sort

looking for the "Selection Sort"-Algorithm in JavaScript

Sort an Array

Given an array of integers nums, sort the array in ascending order.

Input: [5,2,3,1]
Output: [1,2,3,5]

Note:

1 <= A.length <= 10000
-50000 <= A[i] <= 50000

Code:

/**
 * @param {number[]} nums
 * @return {number[]}
 */
var sortArray = function(nums) {
    
};

See Problem here

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.