提交 2a2af410 编写于 作者: L luzhipeng

4道题目

上级 d53aa4cd
## 题目地址
https://leetcode.com/problems/odd-even-linked-list/description/
## 题目描述
```
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.
You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity.
Example 1:
Input: 1->2->3->4->5->NULL
Output: 1->3->5->2->4->NULL
Example 2:
Input: 2->1->3->5->6->4->7->NULL
Output: 2->3->6->7->1->5->4->NULL
Note:
The relative order inside both the even and odd groups should remain as it was in the input.
The first node is considered odd, the second node even and so on ...
```
## 思路
符合直觉的想法是,先遍历一遍找出奇数的节点。然后再遍历一遍找出偶数节点,最后串起来。
但是有两个问题,如果不修改节点的话,需要借助额外的空间,空间复杂度是N。如果修改的话,会对第二次遍历(遍历偶数节点)造成影响。
因此可以采用一种做法: 遍历一次,每一步同时修改两个节点就好了,这样就可以规避上面两个问题。
## 关键点解析
- 用虚拟节点来简化操作
- 循环的结束条件设置为 `odd && odd.next && even && even.next`, 不应该是`odd && even`, 否则需要记录一下奇数节点的最后一个节点,复杂了操作
## 代码
```js
/*
* @lc app=leetcode id=328 lang=javascript
*
* [328] Odd Even Linked List
*
* https://leetcode.com/problems/odd-even-linked-list/description/
*
* algorithms
* Medium (48.22%)
* Total Accepted: 137.6K
* Total Submissions: 284.2K
* Testcase Example: '[1,2,3,4,5]'
*
* 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.
*
* You should try to do it in place. The program should run in O(1) space
* complexity and O(nodes) time complexity.
*
* Example 1:
*
*
* Input: 1->2->3->4->5->NULL
* Output: 1->3->5->2->4->NULL
*
*
* Example 2:
*
*
* Input: 2->1->3->5->6->4->7->NULL
* Output: 2->3->6->7->1->5->4->NULL
*
*
* Note:
*
*
* The relative order inside both the even and odd groups should remain as it
* was in the input.
* The first node is considered odd, the second node even and so on ...
*
*
*/
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var oddEvenList = function(head) {
if (!head || !head.next) return head;
const dummyHead1 = {
next: head
}
const dummyHead2 = {
next: head.next
}
let odd = dummyHead1.next;
let even = dummyHead2.next;
while(odd && odd.next && even && even.next) {
const oddNext = odd.next.next;
const evenNext = even.next.next;
odd.next = oddNext;
even.next = evenNext;
odd = oddNext;
even = evenNext;
}
odd.next = dummyHead2.next;
return dummyHead1.next;
};
```
## 题目地址
https://leetcode.com/problems/intersection-of-two-arrays/description/
## 题目描述
```
Given two arrays, write a function to compute their intersection.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]
Example 2:
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [9,4]
Note:
Each element in the result must be unique.
The result can be in any order.
```
## 思路
先遍历第一个数组,将其存到hashtable中,
然后遍历第二个数组,如果在hashtable中存在就push到return,然后清空hashtable即可。
## 关键点解析
## 代码
```js
/*
* @lc app=leetcode id=349 lang=javascript
*
* [349] Intersection of Two Arrays
*
* https://leetcode.com/problems/intersection-of-two-arrays/description/
*
* algorithms
* Easy (53.11%)
* Total Accepted: 203.6K
* Total Submissions: 380.9K
* Testcase Example: '[1,2,2,1]\n[2,2]'
*
* Given two arrays, write a function to compute their intersection.
*
* Example 1:
*
*
* Input: nums1 = [1,2,2,1], nums2 = [2,2]
* Output: [2]
*
*
*
* Example 2:
*
*
* Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
* Output: [9,4]
*
*
* Note:
*
*
* Each element in the result must be unique.
* The result can be in any order.
*
*
*
*
*/
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number[]}
*/
var intersection = function(nums1, nums2) {
const visited = {};
const ret = [];
for(let i = 0; i < nums1.length; i++) {
const num = nums1[i];
visited[num] = num;
}
for(let i = 0; i < nums2.length; i++) {
const num = nums2[i];
if (visited[num] !== undefined) {
ret.push(num);
visited[num] = undefined;
}
}
return ret;
};
```
## 题目地址
https://leetcode.com/problems/add-two-numbers-ii/description/
## 题目描述
```
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.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
Example:
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7
```
## 思路
由于需要从低位开始加,然后进位。 因此可以采用栈来简化操作。
依次将两个链表的值分别入栈stack1和stack2,然后相加入栈stack,进位操作用一个变量carried记录即可。
最后根据stack生成最终的链表即可。
## 关键点解析
- 栈的基本操作
- carried 变量记录进位
- 循环的终止条件设置成`stack.length > 0` 可以简化操作
- 注意特殊情况, 比如 1 + 99 = 100
## 代码
```js
/*
* @lc app=leetcode id=445 lang=javascript
*
* [445] Add Two Numbers II
*
* https://leetcode.com/problems/add-two-numbers-ii/description/
*
* algorithms
* Medium (49.31%)
* Total Accepted: 83.7K
* Total Submissions: 169K
* Testcase Example: '[7,2,4,3]\n[5,6,4]'
*
* 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.
*
* You may assume the two numbers do not contain any leading zero, except the
* number 0 itself.
*
* Follow up:
* What if you cannot modify the input lists? In other words, reversing the
* lists is not allowed.
*
*
*
* Example:
*
* Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
* Output: 7 -> 8 -> 0 -> 7
*
*
*/
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var addTwoNumbers = function(l1, l2) {
const stack1 = [];
const stack2 = [];
const stack = [];
let cur1 = l1;
let cur2 = l2;
let curried = 0;
while(cur1) {
stack1.push(cur1.val);
cur1 = cur1.next;
}
while(cur2) {
stack2.push(cur2.val);
cur2 = cur2.next;
}
let a = null;
let b = null;
while(stack1.length > 0 || stack2.length > 0) {
a = Number(stack1.pop()) || 0;
b = Number(stack2.pop()) || 0;
stack.push((a + b + curried) % 10);
if (a + b + curried >= 10) {
curried = 1;
} else {
curried = 0;
}
}
if (curried === 1) {
stack.push(1);
}
const dummy = {};
let current = dummy;
while(stack.length > 0) {
current.next = {
val: stack.pop(),
next: null
}
current = current.next
}
return dummy.next;
};
```
## 题目地址
https://leetcode.com/problems/stone-game/description/
## 题目描述
```
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Note:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
```
## 思路
由于piles是偶数的,并且piles的总和是奇数的。
因此Alex`可以做到`要不拿的全部是奇数,要么全部是偶数。
举个例子: 比如Alex第一次先拿第一个
这里有两种情况:
1. Lee如果拿了第二块(偶数),那么Alex继续拿第三块,以此类推。。。
2. Lee如果拿了最后一块(偶数),那么Alex继续拿倒数第二块,以此类推。。。
因此Alex`可以`做到只拿奇数或者偶数,只是他可以控制的,因此他要做的就是数一下,奇数加起来多还是偶数加起来多就好了。
奇数多就全部选奇数,偶数就全部选偶数。 Lee是没有这种自由权的。
## 关键点解析
- 可以用DP(动态规划)
- 可以从数学的角度去分析
> ......(😅)
## 代码
```js
/*
* @lc app=leetcode id=877 lang=javascript
*
* [877] Stone Game
*
* https://leetcode.com/problems/stone-game/description/
*
* algorithms
* Medium (60.46%)
* Total Accepted: 21.4K
* Total Submissions: 35.3K
* Testcase Example: '[5,3,4,5]'
*
* Alex and Lee play a game with piles of stones.  There are an even number of
* piles arranged in a row, and each pile has a positive integer number of
* stones piles[i].
*
* The objective of the game is to end with the most stones.  The total number
* of stones is odd, so there are no ties.
*
* Alex and Lee take turns, with Alex starting first.  Each turn, a player
* takes the entire pile of stones from either the beginning or the end of the
* row.  This continues until there are no more piles left, at which point the
* person with the most stones wins.
*
* Assuming Alex and Lee play optimally, return True if and only if Alex wins
* the game.
*
*
*
* Example 1:
*
*
* Input: [5,3,4,5]
* Output: true
* Explanation:
* Alex starts first, and can only take the first 5 or the last 5.
* Say he takes the first 5, so that the row becomes [3, 4, 5].
* If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10
* points.
* If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win
* with 9 points.
* This demonstrated that taking the first 5 was a winning move for Alex, so we
* return true.
*
*
*
*
* Note:
*
*
* 2 <= piles.length <= 500
* piles.length is even.
* 1 <= piles[i] <= 500
* sum(piles) is odd.
*
*
*
*/
/**
* @param {number[]} piles
* @return {boolean}
*/
var stoneGame = function(piles) {
return true;
};
```
......@@ -11,6 +11,7 @@ leetcode题解,记录自己的leecode解题之路。
- [203.remove-linked-list-elements](./203.remove-linked-list-elements.md)
- [219.contains-duplicate-ii](./219.contains-duplicate-ii.md)
- [283.move-zeroes](./283.move-zeroes.md)
- [349.intersection-of-two-arrays](./349.intersection-of-two-arrays.md)
### 中等难度
- [2. Add Two Numbers](https://github.com/azl397985856/leetcode/blob/master/addTwoNumbers.md)
......@@ -26,6 +27,9 @@ leetcode题解,记录自己的leecode解题之路。
- [103.binary-tree-zigzag-level-order-traversal](./103.binary-tree-zigzag-level-order-traversal.md)
- [144.binary-tree-preorder-traversal](./144.binary-tree-preorder-traversal.md)
- [150.evaluate-reverse-polish-notation](./150.evaluate-reverse-polish-notation.md)
- [328.odd-even-linked-list](./328.odd-even-linked-list.md)
- [445.add-two-numbers-ii](./445.add-two-numbers-ii.md)
- [877.stone-game](./877.stone-game.md)
### 高级难度
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册