Skip to content

Commit 2a2af41

Browse files
author
luzhipeng
committed
4道题目
1 parent d53aa4c commit 2a2af41

5 files changed

+502
-0
lines changed

Diff for: 328.odd-even-linked-list.md

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
2+
## 题目地址
3+
https://leetcode.com/problems/odd-even-linked-list/description/
4+
5+
## 题目描述
6+
7+
```
8+
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.
9+
10+
You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity.
11+
12+
Example 1:
13+
14+
Input: 1->2->3->4->5->NULL
15+
Output: 1->3->5->2->4->NULL
16+
Example 2:
17+
18+
Input: 2->1->3->5->6->4->7->NULL
19+
Output: 2->3->6->7->1->5->4->NULL
20+
Note:
21+
22+
The relative order inside both the even and odd groups should remain as it was in the input.
23+
The first node is considered odd, the second node even and so on ...
24+
```
25+
26+
## 思路
27+
符合直觉的想法是,先遍历一遍找出奇数的节点。然后再遍历一遍找出偶数节点,最后串起来。
28+
29+
但是有两个问题,如果不修改节点的话,需要借助额外的空间,空间复杂度是N。如果修改的话,会对第二次遍历(遍历偶数节点)造成影响。
30+
31+
因此可以采用一种做法: 遍历一次,每一步同时修改两个节点就好了,这样就可以规避上面两个问题。
32+
## 关键点解析
33+
34+
- 用虚拟节点来简化操作
35+
36+
- 循环的结束条件设置为 `odd && odd.next && even && even.next`, 不应该是`odd && even`, 否则需要记录一下奇数节点的最后一个节点,复杂了操作
37+
38+
## 代码
39+
```js
40+
/*
41+
* @lc app=leetcode id=328 lang=javascript
42+
*
43+
* [328] Odd Even Linked List
44+
*
45+
* https://leetcode.com/problems/odd-even-linked-list/description/
46+
*
47+
* algorithms
48+
* Medium (48.22%)
49+
* Total Accepted: 137.6K
50+
* Total Submissions: 284.2K
51+
* Testcase Example: '[1,2,3,4,5]'
52+
*
53+
* Given a singly linked list, group all odd nodes together followed by the
54+
* even nodes. Please note here we are talking about the node number and not
55+
* the value in the nodes.
56+
*
57+
* You should try to do it in place. The program should run in O(1) space
58+
* complexity and O(nodes) time complexity.
59+
*
60+
* Example 1:
61+
*
62+
*
63+
* Input: 1->2->3->4->5->NULL
64+
* Output: 1->3->5->2->4->NULL
65+
*
66+
*
67+
* Example 2:
68+
*
69+
*
70+
* Input: 2->1->3->5->6->4->7->NULL
71+
* Output: 2->3->6->7->1->5->4->NULL
72+
*
73+
*
74+
* Note:
75+
*
76+
*
77+
* The relative order inside both the even and odd groups should remain as it
78+
* was in the input.
79+
* The first node is considered odd, the second node even and so on ...
80+
*
81+
*
82+
*/
83+
/**
84+
* Definition for singly-linked list.
85+
* function ListNode(val) {
86+
* this.val = val;
87+
* this.next = null;
88+
* }
89+
*/
90+
/**
91+
* @param {ListNode} head
92+
* @return {ListNode}
93+
*/
94+
var oddEvenList = function(head) {
95+
if (!head || !head.next) return head;
96+
97+
const dummyHead1 = {
98+
next: head
99+
}
100+
const dummyHead2 = {
101+
next: head.next
102+
}
103+
104+
let odd = dummyHead1.next;
105+
let even = dummyHead2.next;
106+
107+
while(odd && odd.next && even && even.next) {
108+
const oddNext = odd.next.next;
109+
const evenNext = even.next.next;
110+
111+
odd.next = oddNext;
112+
even.next = evenNext;
113+
114+
odd = oddNext;
115+
even = evenNext;
116+
}
117+
118+
odd.next = dummyHead2.next;
119+
120+
return dummyHead1.next;
121+
122+
};
123+
```
124+

Diff for: 349.intersection-of-two-arrays.md

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
2+
## 题目地址
3+
https://leetcode.com/problems/intersection-of-two-arrays/description/
4+
5+
## 题目描述
6+
7+
```
8+
Given two arrays, write a function to compute their intersection.
9+
10+
Example 1:
11+
12+
Input: nums1 = [1,2,2,1], nums2 = [2,2]
13+
Output: [2]
14+
Example 2:
15+
16+
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
17+
Output: [9,4]
18+
Note:
19+
20+
Each element in the result must be unique.
21+
The result can be in any order.
22+
23+
```
24+
25+
## 思路
26+
27+
先遍历第一个数组,将其存到hashtable中,
28+
然后遍历第二个数组,如果在hashtable中存在就push到return,然后清空hashtable即可。
29+
30+
## 关键点解析
31+
32+
33+
34+
## 代码
35+
```js
36+
/*
37+
* @lc app=leetcode id=349 lang=javascript
38+
*
39+
* [349] Intersection of Two Arrays
40+
*
41+
* https://leetcode.com/problems/intersection-of-two-arrays/description/
42+
*
43+
* algorithms
44+
* Easy (53.11%)
45+
* Total Accepted: 203.6K
46+
* Total Submissions: 380.9K
47+
* Testcase Example: '[1,2,2,1]\n[2,2]'
48+
*
49+
* Given two arrays, write a function to compute their intersection.
50+
*
51+
* Example 1:
52+
*
53+
*
54+
* Input: nums1 = [1,2,2,1], nums2 = [2,2]
55+
* Output: [2]
56+
*
57+
*
58+
*
59+
* Example 2:
60+
*
61+
*
62+
* Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
63+
* Output: [9,4]
64+
*
65+
*
66+
* Note:
67+
*
68+
*
69+
* Each element in the result must be unique.
70+
* The result can be in any order.
71+
*
72+
*
73+
*
74+
*
75+
*/
76+
/**
77+
* @param {number[]} nums1
78+
* @param {number[]} nums2
79+
* @return {number[]}
80+
*/
81+
var intersection = function(nums1, nums2) {
82+
const visited = {};
83+
const ret = [];
84+
for(let i = 0; i < nums1.length; i++) {
85+
const num = nums1[i];
86+
87+
visited[num] = num;
88+
}
89+
90+
for(let i = 0; i < nums2.length; i++) {
91+
const num = nums2[i];
92+
93+
if (visited[num] !== undefined) {
94+
ret.push(num);
95+
visited[num] = undefined;
96+
}
97+
}
98+
99+
return ret;
100+
101+
};
102+
```
103+

Diff for: 445.add-two-numbers-ii.md

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
2+
## 题目地址
3+
https://leetcode.com/problems/add-two-numbers-ii/description/
4+
5+
## 题目描述
6+
7+
```
8+
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
9+
10+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
11+
12+
Follow up:
13+
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
14+
15+
Example:
16+
17+
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
18+
Output: 7 -> 8 -> 0 -> 7
19+
20+
```
21+
## 思路
22+
23+
由于需要从低位开始加,然后进位。 因此可以采用栈来简化操作。
24+
依次将两个链表的值分别入栈stack1和stack2,然后相加入栈stack,进位操作用一个变量carried记录即可。
25+
26+
最后根据stack生成最终的链表即可。
27+
28+
## 关键点解析
29+
30+
- 栈的基本操作
31+
- carried 变量记录进位
32+
- 循环的终止条件设置成`stack.length > 0` 可以简化操作
33+
- 注意特殊情况, 比如 1 + 99 = 100
34+
35+
## 代码
36+
```js
37+
/*
38+
* @lc app=leetcode id=445 lang=javascript
39+
*
40+
* [445] Add Two Numbers II
41+
*
42+
* https://leetcode.com/problems/add-two-numbers-ii/description/
43+
*
44+
* algorithms
45+
* Medium (49.31%)
46+
* Total Accepted: 83.7K
47+
* Total Submissions: 169K
48+
* Testcase Example: '[7,2,4,3]\n[5,6,4]'
49+
*
50+
* You are given two non-empty linked lists representing two non-negative
51+
* integers. The most significant digit comes first and each of their nodes
52+
* contain a single digit. Add the two numbers and return it as a linked list.
53+
*
54+
* You may assume the two numbers do not contain any leading zero, except the
55+
* number 0 itself.
56+
*
57+
* Follow up:
58+
* What if you cannot modify the input lists? In other words, reversing the
59+
* lists is not allowed.
60+
*
61+
*
62+
*
63+
* Example:
64+
*
65+
* Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
66+
* Output: 7 -> 8 -> 0 -> 7
67+
*
68+
*
69+
*/
70+
/**
71+
* Definition for singly-linked list.
72+
* function ListNode(val) {
73+
* this.val = val;
74+
* this.next = null;
75+
* }
76+
*/
77+
/**
78+
* @param {ListNode} l1
79+
* @param {ListNode} l2
80+
* @return {ListNode}
81+
*/
82+
var addTwoNumbers = function(l1, l2) {
83+
const stack1 = [];
84+
const stack2 = [];
85+
const stack = [];
86+
87+
let cur1 = l1;
88+
let cur2 = l2;
89+
let curried = 0;
90+
91+
while(cur1) {
92+
stack1.push(cur1.val);
93+
cur1 = cur1.next;
94+
}
95+
96+
while(cur2) {
97+
stack2.push(cur2.val);
98+
cur2 = cur2.next;
99+
}
100+
101+
let a = null;
102+
let b = null;
103+
104+
while(stack1.length > 0 || stack2.length > 0) {
105+
a = Number(stack1.pop()) || 0;
106+
b = Number(stack2.pop()) || 0;
107+
108+
stack.push((a + b + curried) % 10);
109+
110+
if (a + b + curried >= 10) {
111+
curried = 1;
112+
} else {
113+
curried = 0;
114+
}
115+
}
116+
117+
if (curried === 1) {
118+
stack.push(1);
119+
}
120+
121+
const dummy = {};
122+
123+
let current = dummy;
124+
125+
while(stack.length > 0) {
126+
current.next = {
127+
val: stack.pop(),
128+
next: null
129+
}
130+
131+
current = current.next
132+
}
133+
134+
return dummy.next;
135+
};
136+
137+
```

0 commit comments

Comments
 (0)