Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 78aebbe

Browse files
update 4 leetcode
1 parent 6e39eef commit 78aebbe

File tree

4 files changed

+216
-0
lines changed

4 files changed

+216
-0
lines changed

‎leetcode/141.linked-list-cycle.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* @lc app=leetcode id=141 lang=javascript
3+
*
4+
* [141] Linked List Cycle
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for singly-linked list.
10+
* function ListNode(val) {
11+
* this.val = val;
12+
* this.next = null;
13+
* }
14+
*/
15+
16+
/**
17+
* 快慢指针
18+
* @param {ListNode} head
19+
* @return {boolean}
20+
*/
21+
var hasCycle = function(head) {
22+
if (head === null || head.next === null) {
23+
return false;
24+
}
25+
let slow = fast = head;
26+
fast = fast.next;
27+
28+
// 如果快指针的下一个节点和慢指针重合,则说明形成了环
29+
while (slow !== null) {
30+
if (fast === null || fast.next === null) {
31+
return false;
32+
}
33+
if (slow === fast.next) {
34+
return true;
35+
}
36+
37+
slow = slow.next;
38+
fast = fast.next.next;
39+
}
40+
};
41+
42+
/**
43+
* 哈希表的方式 O(n)
44+
* @param {*} head
45+
* @returns
46+
*/
47+
var hasCycle2 = function(head) {
48+
let map = new Map();
49+
50+
while (head !== null) {
51+
if (map.get(head.val) === head.next) {
52+
return true;
53+
}
54+
map.set(head.val, head.next);
55+
head = head.next;
56+
}
57+
return false;
58+
};
59+
// @lc code=end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* @lc app=leetcode id=19 lang=javascript
3+
*
4+
* [19] Remove Nth Node From End of List
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for singly-linked list.
10+
* function ListNode(val, next) {
11+
* this.val = (val===undefined ? 0 : val)
12+
* this.next = (next===undefined ? null : next)
13+
* }
14+
*/
15+
/**
16+
* 可以遍历一次获取索引,再遍历得到倒数索引
17+
* 也可以使用双指针,快慢指针的方式
18+
* @param {ListNode} head
19+
* @param {number} n
20+
* @return {ListNode}
21+
*/
22+
var removeNthFromEnd = function(head, n) {
23+
let first = head;
24+
let prev = new ListNode(0, head);
25+
let second = prev;
26+
27+
// 第一个链表先遍历到 n 的位置,再往后遍历,就正好是 length - n 的数据
28+
for (let i = 0; i < n; i++) {
29+
first = first.next;
30+
}
31+
32+
// 第一个链表到结尾的时候,第二个正好到倒数 n 的位置
33+
while (first !== null) {
34+
first = first.next;
35+
second = second.next;
36+
}
37+
38+
// 删除节点
39+
second.next = second.next.next;
40+
41+
// 返回删除之后的链表
42+
const ans = prev.next;
43+
return ans;
44+
};
45+
// @lc code=end

‎leetcode/206.reverse-linked-list.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* @lc app=leetcode id=206 lang=javascript
3+
*
4+
* [206] Reverse Linked List
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for singly-linked list.
10+
* function ListNode(val, next) {
11+
* this.val = (val===undefined ? 0 : val)
12+
* this.next = (next===undefined ? null : next)
13+
* }
14+
*/
15+
/**
16+
* 迭代的方式
17+
* @param {ListNode} head
18+
* @return {ListNode}
19+
*/
20+
var reverseList = function(head) {
21+
let curr = head;
22+
let prev = null;
23+
24+
while (curr !== null) {
25+
// 保存下一节点,后面这个指针会改变
26+
const next = curr.next;
27+
// 反过来把下一节点保存到前一个
28+
curr.next = prev;
29+
// 把当前节点当做是前一个几点
30+
prev = curr;
31+
// 往后迭代原来的节点
32+
curr = next;
33+
}
34+
35+
return prev;
36+
};
37+
// @lc code=end

‎leetcode/697.degree-of-an-array.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* @lc app=leetcode id=697 lang=javascript
3+
*
4+
* [697] Degree of an Array
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {number[]} nums
10+
* @return {number}
11+
*/
12+
var findShortestSubArray = function(nums) {
13+
// 遍历保存所有数的数量及索引
14+
let map = new Map();
15+
// 保存所有数的数量,数量和相应的出现索引
16+
for (let i = 0; i < nums.length; i++) {
17+
const ele = nums[i];
18+
let current = map.get(ele) || {};
19+
current.count = (current.count || 0) + 1;
20+
current.firstIndex = current.firstIndex !== undefined ? current.firstIndex : i;
21+
current.lastIndex = i;
22+
map.set(ele, current);
23+
}
24+
25+
// 保存数组的度,和对应度的数组长度,比下面节约使用了数组
26+
let maxCount = 0;
27+
let minLength = Number.MAX_SAFE_INTEGER;
28+
// 注意可能会有多个数组的度
29+
for (const [key, obj] of map) {
30+
if (obj.count > maxCount) {
31+
// 数组的长度是差值加 1
32+
minLength = obj.lastIndex - obj.firstIndex + 1;
33+
maxCount = obj.count;
34+
}
35+
if (obj.count === maxCount) {
36+
minLength = (obj.lastIndex - obj.firstIndex + 1) < minLength ? (obj.lastIndex - obj.firstIndex + 1) : minLength;
37+
}
38+
}
39+
40+
return minLength;
41+
};
42+
43+
/**
44+
* 数组的方式保存
45+
* @param {*} nums
46+
* @returns
47+
*/
48+
var findShortestSubArray2 = function(nums) {
49+
// 遍历保存所有数的数量及索引
50+
let map = new Map();
51+
// 保存所有数的数量,数量和相应的出现索引
52+
for (let i = 0; i < nums.length; i++) {
53+
const ele = nums[i];
54+
let current = map.get(ele) || {};
55+
current.count = (current.count || 0) + 1;
56+
current.firstIndex = current.firstIndex !== undefined ? current.firstIndex : i;
57+
current.lastIndex = i;
58+
map.set(ele, current);
59+
}
60+
61+
let maxEle = [{ count: 0 }];
62+
// 保存数组的度的索引,注意可能会有多个数组的度,所以需要数组来保存
63+
for (const [key, obj] of map) {
64+
if (obj.count > maxEle[0].count) {
65+
maxEle = [obj];
66+
} else if (obj.count === maxEle[0].count) {
67+
maxEle.push(obj);
68+
}
69+
}
70+
71+
return maxEle.reduce((prev, cur) => {
72+
return (cur.lastIndex - cur.firstIndex + 1) < prev ? (cur.lastIndex - cur.firstIndex + 1) : prev;
73+
}, Number.MAX_SAFE_INTEGER);
74+
};
75+
// @lc code=end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /