Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e4927b5

Browse files
committed
docs: 新增No.155、No.160、No.169题解
1 parent 1d37c98 commit e4927b5

File tree

4 files changed

+242
-0
lines changed

4 files changed

+242
-0
lines changed

leetcode刷题/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
- [No.125 验证回文串](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No125_is-palindrome.md)
1818
- [No.136 只出现一次的数字](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No136_single-number.md)
1919
- [No.141 环形链表](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No141_has_cycle.md)
20+
- [No.155 最小栈](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No155_min-stack.md)
21+
- [No.160 相交链表](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No160_get-intersection-node.md)
22+
- [No.169 多数元素](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No169_majority-element.md)
2023
- [No.189 旋转数组](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No189_rotate-arr.md)
2124
- [No.206 反转链表](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No206_reverse-list.md)
2225
- [No.234 回文链表](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No234_is-palindrome.md)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# No.155 最小栈
2+
3+
设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。
4+
5+
push(x) -- 将元素 x 推入栈中。
6+
pop() -- 删除栈顶的元素。
7+
top() -- 获取栈顶元素。
8+
getMin() -- 检索栈中的最小元素。
9+
10+
## 示例
11+
12+
示例:
13+
```
14+
MinStack minStack = new MinStack();
15+
minStack.push(-2);
16+
minStack.push(0);
17+
minStack.push(-3);
18+
minStack.getMin(); --> 返回 -3.
19+
minStack.pop();
20+
minStack.top(); --> 返回 0.
21+
minStack.getMin(); --> 返回 -2.
22+
```
23+
24+
## 解题思路
25+
26+
js直接使用数组实现栈,最小值通过遍历查找,代码如下:
27+
28+
```javascript
29+
/**
30+
* initialize your data structure here.
31+
*/
32+
var MinStack = function() {
33+
this.stack = [];
34+
this.min = 0;
35+
};
36+
37+
/**
38+
* @param {number} x
39+
* @return {void}
40+
*/
41+
MinStack.prototype.push = function(x) {
42+
if (this.stack[this.stack.length - 1] )
43+
this.stack.push(x);
44+
};
45+
46+
/**
47+
* @return {void}
48+
*/
49+
MinStack.prototype.pop = function() {
50+
this.stack.pop();
51+
};
52+
53+
/**
54+
* @return {number}
55+
*/
56+
MinStack.prototype.top = function() {
57+
return this.stack[this.stack.length - 1];
58+
};
59+
60+
/**
61+
* @return {number}
62+
*/
63+
MinStack.prototype.getMin = function() {
64+
let min = this.stack[0];
65+
for (let i = 1; i < this.stack.length; i++) {
66+
if (this.stack[i] < min) {
67+
min = this.stack[i];
68+
}
69+
}
70+
return min;
71+
};
72+
73+
/**
74+
* Your MinStack object will be instantiated and called as such:
75+
* var obj = new MinStack()
76+
* obj.push(x)
77+
* obj.pop()
78+
* var param_3 = obj.top()
79+
* var param_4 = obj.getMin()
80+
*/
81+
```
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# No.160 相交链表
2+
3+
难度:`easy`
4+
5+
编写一个程序,找到两个单链表相交的起始节点。
6+
7+
## 示例
8+
9+
示例 1
10+
```
11+
输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
12+
输出:Reference of the node with value = 8
13+
输入解释:相交节点的值为 8 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。
14+
```
15+
16+
示例 2:
17+
```
18+
输入:intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
19+
输出:Reference of the node with value = 2
20+
输入解释:相交节点的值为 2 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。
21+
``` 
22+
23+
示例 3:
24+
```
25+
输入:intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
26+
输出:null
27+
输入解释:从各自的表头开始算起,链表 A 为 [2,6,4],链表 B 为 [1,5]。由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。
28+
解释:这两个链表不相交,因此返回 null。
29+
``` 
30+
31+
32+
注意:
33+
34+
```
35+
如果两个链表没有交点,返回 null.
36+
在返回结果后,两个链表仍须保持原有的结构。
37+
可假定整个链表结构中没有循环。
38+
程序尽量满足 O(n) 时间复杂度,且仅用 O(1) 内存。
39+
```
40+
41+
## 解题思路
42+
43+
使用暴力法,遍历 A 的同时,遍历 B 链表,如果有相同的节点,直接返回。
44+
45+
这里使用另一种解法,首先分别遍历链表 A 和 B,计算出链表长度之差为n。
46+
47+
使较长的链表先遍历n,这时候两个链表到链表尾部的距离是相同的,然后同时遍历,通过判断节点是否相同,如果相同直接返回。
48+
49+
代码如下:
50+
51+
```javascript
52+
/**
53+
* Definition for singly-linked list.
54+
* function ListNode(val) {
55+
* this.val = val;
56+
* this.next = null;
57+
* }
58+
*/
59+
60+
/**
61+
* @param {ListNode} headA
62+
* @param {ListNode} headB
63+
* @return {ListNode}
64+
*/
65+
var getIntersectionNode = function(headA, headB) {
66+
let lengthA=lengthB=0;
67+
let curA = headA;
68+
let curB = headB;
69+
while (curA) {
70+
curA = curA.next;
71+
lengthA++;
72+
}
73+
while (curB) {
74+
curB = curB.next;
75+
lengthB++;
76+
}
77+
let interval = lengthA - lengthB;
78+
curA = headA;
79+
curB = headB;
80+
81+
if (interval > 0) {
82+
while (interval) {
83+
curA = curA.next;
84+
interval--;
85+
}
86+
} else {
87+
while (interval < 0) {
88+
curB = curB.next;
89+
interval++;
90+
}
91+
}
92+
93+
while (curA || curB) {
94+
if (curA == curB) {
95+
return curA;
96+
}
97+
curA = curA.next;
98+
curB = curB.next;
99+
}
100+
return null;
101+
};
102+
```
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# No.169 多数元素
2+
3+
难度:`easy`
4+
5+
给定一个大小为 n 的数组,找到其中的多数元素。多数元素是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。
6+
7+
你可以假设数组是非空的,并且给定的数组总是存在多数元素。
8+
9+
## 示例
10+
11+
示例 1:
12+
```
13+
输入: [3,2,3]
14+
输出: 3
15+
```
16+
17+
18+
示例 2:
19+
```
20+
输入: [2,2,1,1,1,2,2]
21+
输出: 2
22+
```
23+
24+
## 解题思路
25+
26+
首先想到的是使用 Map 的方法,遍历整个数组,对每个数组出现的个数进行计数。
27+
28+
代码如下:
29+
30+
```javascript
31+
/**
32+
* @param {number[]} nums
33+
* @return {number}
34+
*/
35+
var majorityElement = function(nums) {
36+
let cond = nums.length / 2;
37+
// 使用 map,记录每个数的出现的个数
38+
let map = new Map();
39+
40+
nums.map(item => {
41+
let count = map.get(item) || 0;
42+
count++;
43+
map.set(item, count);
44+
});
45+
46+
for (let [key, value] of map) {
47+
if (value > cond) {
48+
return key;
49+
}
50+
}
51+
};
52+
```
53+
54+
另外一种解题思路,可以通过排序,排序后数组中间的数一定是所要找的数字。
55+
56+
代码:略

0 commit comments

Comments
 (0)