Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 402f1a8

Browse files
committedOct 31, 2020
add js solution for leetcode 21 344 387
1 parent 31fb5ca commit 402f1a8

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed
 

‎README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
LeetCode
2-
========
1+
# LeetCode
32

4-
### LeetCode Algorithm
3+
## LeetCode Algorithm
54

65
| # | Title | Solution | Difficulty |
76
|---| ----- | -------- | ---------- |
@@ -116,7 +115,7 @@ LeetCode
116115
|390|[Elimination Game](https://leetcode.com/problems/elimination-game/) | |Medium|
117116
|389|[Find the Difference](https://leetcode.com/problems/find-the-difference/) | |Easy|
118117
|388|[Longest Absolute File Path](https://leetcode.com/problems/longest-absolute-file-path/) | |Medium|
119-
|387|[First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | |Easy|
118+
|387|[First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [js](./algorithms/firstUniqueCharacterInAString/firstUniqueCharacterInAString.js) |Easy|
120119
|386|[Lexicographical Numbers](https://leetcode.com/problems/lexicographical-numbers/) | |Medium|
121120
|385|[Mini Parser](https://leetcode.com/problems/mini-parser/) | |Medium|
122121
|384|[Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/) | |Medium|
@@ -133,7 +132,7 @@ LeetCode
133132
|349|[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) | [js](./algorithms/intersectionOfTwoArrays/intersectionOfTwoArrays.js) |Easy|
134133
|347|[Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | |Medium|
135134
|345|[Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/) | |Easy|
136-
|344|[Reverse String](https://leetcode.com/problems/reverse-string/) [java](./algorithms/resverseString/Solution.java) | |Easy|
135+
|344|[Reverse String](https://leetcode.com/problems/reverse-string/) [java](./algorithms/resverseString/Solution.java), [js](./algorithms/resverseString/resverseString.js) | |Easy|
137136
|343|[Integer Break](https://leetcode.com/problems/integer-break/) | |Medium|
138137
|342|[Power of Four](https://leetcode.com/problems/power-of-four/) | |Easy|
139138
|341|[Flatten Nested List Iterator](https://leetcode.com/problems/flatten-nested-list-iterator/) | |Medium|
@@ -396,7 +395,7 @@ LeetCode
396395
|24|[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/)| [js](./algorithms/swapNodesInPairs/swapNodesInPairs.js) |Medium|
397396
|23|[Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/)| |Hard|
398397
|22|[Generate Parentheses](https://leetcode.com/problems/generate-parentheses/)| [js](./algorithms/generateParentheses/generateParentheses.js) |Medium|
399-
|21|[Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/)| [java](./algorithm/mergeTwoSortedLists/Solution.java) |Easy|
398+
|21|[Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/)| [java](./algorithm/mergeTwoSortedLists/Solution.java), [js](./algorithms/mergeTwoSortedLists/mergeTwoSortedLists.js) |Easy|
400399
|20|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/)| [js](./algorithms/validParentheses/validParentheses.js) |Easy|
401400
|19|[Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/)| [js](./algorithms/removeNthNodeFromEndOfList/removeNthNodeFromEndOfList.js) |Easy|
402401
|18|[4Sum](https://leetcode.com/problems/4sum/)| [js](./algorithms/4Sum/4Sum.js) |Medium|
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var firstUniqChar = function(s) {
2+
const startIndex = 'a'.charCodeAt();
3+
// 创建一个长度为26的字母表,初始频率都为0
4+
const freq = Array.from({length: 26}, x => 0);
5+
// 与a做比较得到索引位置,如果重复则频率+1
6+
for (let i = 0; i < s.length; i++) {
7+
freq[s.charCodeAt(i) - startIndex] ++;
8+
}
9+
// 最后再扫一遍s,如果在频率表中的频率为1,则代表只出现过一次
10+
for (let i = 0; i < s.length; i++) {
11+
if (freq[s.charCodeAt(i) - startIndex] === 1) {
12+
return i;
13+
}
14+
}
15+
return -1;
16+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var mergeTwoLists = function(l1, l2) {
2+
const prehead = new ListNode(-1);
3+
let prev = prehead;
4+
while (l1 !== null && l2 !== null) {
5+
// l1和l2谁小就接在后面,同时当前指针后移一位
6+
if (l1.val <= l2.val) {
7+
prev.next = l1;
8+
l1 = l1.next;
9+
} else {
10+
prev.next = l2;
11+
l2 = l2.next;
12+
}
13+
// 不管将哪一个元素接在了后面,我都需要把 prev 向后移一位。
14+
prev = prev.next;
15+
}
16+
// 合并后 l1 和 l2 最多只有一个还未被合并完,直接将链表末尾指向未合并完的链表即可
17+
prev.next = l1 === null ? l2 : l1;
18+
return prehead.next;
19+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var reverseString = function(s) {
2+
const len = s.length;
3+
const halfLen = Math.round(len/2);
4+
// 只用遍历前一半数组
5+
for (let i = 0; i < halfLen; i++) {
6+
// 两两交换
7+
[s[i], s[len - 1 -i]] = [s[len - 1 -i], s[i]];
8+
}
9+
return s;
10+
};

0 commit comments

Comments
 (0)
Please sign in to comment.