Skip to content

Commit 4ccd1c4

Browse files
committed
feat: add 2 solutions
0 parents  commit 4ccd1c4

File tree

7 files changed

+1622
-0
lines changed

7 files changed

+1622
-0
lines changed

.gitignore

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
# Numerous always-ignore extensions
3+
*.bak
4+
*.patch
5+
*.diff
6+
*.err
7+
8+
# temp file for git conflict merging
9+
*.orig
10+
*.log
11+
*.rej
12+
*.swo
13+
*.swp
14+
*.zip
15+
*.vi
16+
*~
17+
*.sass-cache
18+
*.tmp.html
19+
*.dump
20+
21+
# OS or Editor folders
22+
.DS_Store
23+
._*
24+
.cache
25+
.project
26+
.settings
27+
.tmproj
28+
*.esproj
29+
*.sublime-project
30+
*.sublime-workspace
31+
nbproject
32+
thumbs.db
33+
*.iml
34+
35+
# Folders to ignore
36+
.hg
37+
.svn
38+
.CVS
39+
.idea
40+
41+
# custom
42+
node_modules/
43+
jscoverage_lib/
44+
bower_components/
45+
.rts2_cache_*
46+
coverage

README.md

Lines changed: 977 additions & 0 deletions
Large diffs are not rendered by default.

algorithms/Add Two Numbers/index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// problem https://leetcode.com/problems/add-two-numbers/
2+
3+
/**
4+
* Definition for singly-linked list.
5+
* function ListNode(val) {
6+
* this.val = val;
7+
* this.next = null;
8+
* }
9+
*/
10+
/**
11+
* @param {ListNode} l1
12+
* @param {ListNode} l2
13+
* @return {ListNode}
14+
*/
15+
const addTwoNumbers = function (l1, l2) {
16+
let head = new ListNode(0)
17+
let node = head
18+
let carry = 0
19+
20+
while (l1 || l2) {
21+
let x = l1 ? l1.val : 0
22+
let y = l2 ? l2.val : 0
23+
let sum = x + y + carry
24+
25+
carry = sum > 9 ? 1 : 0
26+
27+
node.next = new ListNode(sum % 10)
28+
node = node.next
29+
30+
l1 && (l1 = l1.next)
31+
l2 && (l2 = l2.next)
32+
}
33+
34+
if (carry > 0) {
35+
node.next = new ListNode(carry)
36+
}
37+
38+
return head.next
39+
};

algorithms/Two Sum/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// problem https://leetcode.com/problems/two-sum/
2+
3+
/**
4+
* @param {number[]} nums
5+
* @param {number} target
6+
* @return {number[]}
7+
*/
8+
const twoSum = function(nums, target) {
9+
const hashmap = {}
10+
11+
for (let i = 0; i < nums.length; i++) {
12+
let other = target - nums[i]
13+
14+
if (hashmap[other] !== undefined) {
15+
return [hashmap[other], i]
16+
}
17+
18+
hashmap[nums[i]] = i
19+
}
20+
}

package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "leetcode",
3+
"version": "1.0.0",
4+
"description": "LeetCode solutions with JavaScript",
5+
"main": "index.js",
6+
"scripts": {
7+
"gen": "node scripts/generate.js",
8+
"precommit": "npm run gen && git add ."
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git+ssh://[email protected]/Chersquwn/leetcode.git"
13+
},
14+
"keywords": [
15+
"leetcode"
16+
],
17+
"author": "Chersquwn",
18+
"license": "MIT",
19+
"bugs": {
20+
"url": "https://github.com/Chersquwn/leetcode/issues"
21+
},
22+
"homepage": "https://github.com/Chersquwn/leetcode#readme",
23+
"dependencies": {
24+
"axios": "^0.18.0"
25+
},
26+
"devDependencies": {
27+
"husky": "^1.3.1"
28+
}
29+
}

scripts/generate.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
const axios = require('axios');
2+
const fs = require('fs');
3+
4+
const API = 'https://leetcode.com/api/problems/algorithms/';
5+
const SOLUTION_Path = './algorithms/';
6+
let README_TMPL = `# LeetCode
7+
8+
LeetCode solutions with JavaScript
9+
10+
## LeetCode Algorithms
11+
12+
| # | Title | Explanation | Solution | Difficulty |
13+
|:---:|:---:|:---:|:---:|:---:|
14+
`;
15+
16+
fetchProblems()
17+
.then(problems => {
18+
return addSolutions(problems);
19+
})
20+
.then(problems => {
21+
generateReadme(problems);
22+
});
23+
24+
25+
function fetchProblems() {
26+
const time = Date.now()
27+
return axios.get(API).then(res => {
28+
const { stat_status_pairs } = res.data;
29+
const difficulty = ['', 'Easy', 'Medium', 'Hard'];
30+
const problems = stat_status_pairs
31+
.map(item => ({
32+
id: item.stat.question_id,
33+
title: item.stat.question__title,
34+
url: `https://leetcode.com/problems/${item.stat.question__title_slug}/`,
35+
difficulty: difficulty[item.difficulty.level]
36+
}))
37+
.sort((a, b) => b.id - a.id);
38+
return problems;
39+
});
40+
}
41+
42+
function addSolutions(problems) {
43+
const newProblems = problems.map(problem => {
44+
try {
45+
const files = fs.readdirSync(SOLUTION_Path + problem.title);
46+
47+
files.forEach(file => {
48+
if (file.endsWith('js')) {
49+
problem.solution = 'JavaScript';
50+
problem.solutionUrl = encodeURI(
51+
SOLUTION_Path + problem.title + '/' + file
52+
);
53+
}
54+
});
55+
} catch (error) {
56+
console.log(error)
57+
}
58+
59+
return problem;
60+
});
61+
62+
return Promise.resolve(newProblems);
63+
}
64+
65+
function generateReadme(problems) {
66+
problems.forEach(({ id, title, url, difficulty, solution, solutionUrl }) => {
67+
README_TMPL += `| ${id} | [${title}](${url}) | | `
68+
69+
if (solution) {
70+
README_TMPL += `[${solution}](${solutionUrl})`
71+
}
72+
73+
README_TMPL += ` | ${difficulty} | \n`;
74+
});
75+
76+
fs.writeFileSync('README.md', README_TMPL);
77+
78+
console.info('README.md generate successfully');
79+
}

0 commit comments

Comments
 (0)