Skip to content

Commit

Permalink
feat: add 2 solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
Chersquwn committed Apr 6, 2019
0 parents commit 4ccd1c4
Show file tree
Hide file tree
Showing 7 changed files with 1,622 additions and 0 deletions.
46 changes: 46 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

# Numerous always-ignore extensions
*.bak
*.patch
*.diff
*.err

# temp file for git conflict merging
*.orig
*.log
*.rej
*.swo
*.swp
*.zip
*.vi
*~
*.sass-cache
*.tmp.html
*.dump

# OS or Editor folders
.DS_Store
._*
.cache
.project
.settings
.tmproj
*.esproj
*.sublime-project
*.sublime-workspace
nbproject
thumbs.db
*.iml

# Folders to ignore
.hg
.svn
.CVS
.idea

# custom
node_modules/
jscoverage_lib/
bower_components/
.rts2_cache_*
coverage
977 changes: 977 additions & 0 deletions README.md

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions algorithms/Add Two Numbers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// problem https://leetcode.com/problems/add-two-numbers/

/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
const addTwoNumbers = function (l1, l2) {
let head = new ListNode(0)
let node = head
let carry = 0

while (l1 || l2) {
let x = l1 ? l1.val : 0
let y = l2 ? l2.val : 0
let sum = x + y + carry

carry = sum > 9 ? 1 : 0

node.next = new ListNode(sum % 10)
node = node.next

l1 && (l1 = l1.next)
l2 && (l2 = l2.next)
}

if (carry > 0) {
node.next = new ListNode(carry)
}

return head.next
};
20 changes: 20 additions & 0 deletions algorithms/Two Sum/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// problem https://leetcode.com/problems/two-sum/

/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
const twoSum = function(nums, target) {
const hashmap = {}

for (let i = 0; i < nums.length; i++) {
let other = target - nums[i]

if (hashmap[other] !== undefined) {
return [hashmap[other], i]
}

hashmap[nums[i]] = i
}
}
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "leetcode",
"version": "1.0.0",
"description": "LeetCode solutions with JavaScript",
"main": "index.js",
"scripts": {
"gen": "node scripts/generate.js",
"precommit": "npm run gen && git add ."
},
"repository": {
"type": "git",
"url": "git+ssh://[email protected]/Chersquwn/leetcode.git"
},
"keywords": [
"leetcode"
],
"author": "Chersquwn",
"license": "MIT",
"bugs": {
"url": "https://github.com/Chersquwn/leetcode/issues"
},
"homepage": "https://github.com/Chersquwn/leetcode#readme",
"dependencies": {
"axios": "^0.18.0"
},
"devDependencies": {
"husky": "^1.3.1"
}
}
79 changes: 79 additions & 0 deletions scripts/generate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const axios = require('axios');
const fs = require('fs');

const API = 'https://leetcode.com/api/problems/algorithms/';
const SOLUTION_Path = './algorithms/';
let README_TMPL = `# LeetCode
LeetCode solutions with JavaScript
## LeetCode Algorithms
| # | Title | Explanation | Solution | Difficulty |
|:---:|:---:|:---:|:---:|:---:|
`;

fetchProblems()
.then(problems => {
return addSolutions(problems);
})
.then(problems => {
generateReadme(problems);
});


function fetchProblems() {
const time = Date.now()
return axios.get(API).then(res => {
const { stat_status_pairs } = res.data;
const difficulty = ['', 'Easy', 'Medium', 'Hard'];
const problems = stat_status_pairs
.map(item => ({
id: item.stat.question_id,
title: item.stat.question__title,
url: `https://leetcode.com/problems/${item.stat.question__title_slug}/`,
difficulty: difficulty[item.difficulty.level]
}))
.sort((a, b) => b.id - a.id);
return problems;
});
}

function addSolutions(problems) {
const newProblems = problems.map(problem => {
try {
const files = fs.readdirSync(SOLUTION_Path + problem.title);

files.forEach(file => {
if (file.endsWith('js')) {
problem.solution = 'JavaScript';
problem.solutionUrl = encodeURI(
SOLUTION_Path + problem.title + '/' + file
);
}
});
} catch (error) {
console.log(error)
}

return problem;
});

return Promise.resolve(newProblems);
}

function generateReadme(problems) {
problems.forEach(({ id, title, url, difficulty, solution, solutionUrl }) => {
README_TMPL += `| ${id} | [${title}](${url}) | | `

if (solution) {
README_TMPL += `[${solution}](${solutionUrl})`
}

README_TMPL += ` | ${difficulty} | \n`;
});

fs.writeFileSync('README.md', README_TMPL);

console.info('README.md generate successfully');
}
Loading

0 comments on commit 4ccd1c4

Please sign in to comment.