-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4ccd1c4
Showing
7 changed files
with
1,622 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} |
Oops, something went wrong.