Skip to content

Commit 8067920

Browse files
committed
finish 80~84
1 parent af3eefe commit 8067920

5 files changed

+355
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# 80. Remove Duplicates from Sorted Array II
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Two Pointers.
5+
- Similar Questions: Remove Duplicates from Sorted Array.
6+
7+
## Problem
8+
9+
Given a sorted array **nums**, remove the duplicates **in-place** such that duplicates appeared at most **twice** and return the new length.
10+
11+
Do not allocate extra space for another array, you must do this by **modifying the input array in-place** with O(1) extra memory.
12+
13+
**Example 1:**
14+
15+
```
16+
Given nums = [1,1,1,2,2,3],
17+
18+
Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively.
19+
20+
It doesn't matter what you leave beyond the returned length.
21+
```
22+
23+
**Example 2:**
24+
25+
```
26+
Given nums = [0,0,1,1,1,1,2,3,3],
27+
28+
Your function should return length = 7, with the first seven elements of nums being modified to 0, 0, 1, 1, 2, 3 and 3 respectively.
29+
30+
It doesn't matter what values are set beyond the returned length.
31+
```
32+
33+
**Clarification:**
34+
35+
Confused why the returned value is an integer but your answer is an array?
36+
37+
Note that the input array is passed in by **reference**, which means modification to the input array will be known to the caller as well.
38+
39+
Internally you can think of this:
40+
41+
```
42+
// nums is passed in by reference. (i.e., without making a copy)
43+
int len = removeDuplicates(nums);
44+
45+
// any modification to nums in your function would be known by the caller.
46+
// using the length returned by your function, it prints the first len elements.
47+
for (int i = 0; i < len; i++) {
48+
    print(nums[i]);
49+
}
50+
```
51+
52+
## Solution
53+
54+
```javascript
55+
/**
56+
* @param {number[]} nums
57+
* @return {number}
58+
*/
59+
var removeDuplicates = function(nums) {
60+
var len = nums.length;
61+
var index = 0;
62+
var last = NaN;
63+
var times = 0;
64+
for (var i = 0; i < len; i++) {
65+
if (nums[i] === last) {
66+
if (times < 2) times++;
67+
else continue;
68+
} else {
69+
times = 1;
70+
}
71+
last = nums[i];
72+
nums[index] = nums[i];
73+
index++;
74+
}
75+
return index;
76+
};
77+
```
78+
79+
**Explain:**
80+
81+
nope.
82+
83+
**Complexity:**
84+
85+
* Time complexity : O(n).
86+
* Space complexity : O(1).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# 81. Search in Rotated Sorted Array II
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Binary Search.
5+
- Similar Questions: Search in Rotated Sorted Array.
6+
7+
## Problem
8+
9+
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
10+
11+
(i.e., ```[0,0,1,2,2,5,6]``` might become ```[2,5,6,0,0,1,2]```).
12+
13+
You are given a target value to search. If found in the array return ```true```, otherwise return ```false```.
14+
15+
**Example 1:**
16+
17+
```
18+
Input: nums = [2,5,6,0,0,1,2], target = 0
19+
Output: true
20+
```
21+
22+
**Example 2:**
23+
24+
```
25+
Input: nums = [2,5,6,0,0,1,2], target = 3
26+
Output: false
27+
```
28+
29+
**Follow up:**
30+
31+
- This is a follow up problem to Search in Rotated Sorted Array, where ```nums``` may contain duplicates.
32+
- Would this affect the run-time complexity? How and why?
33+
34+
## Solution
35+
36+
```javascript
37+
/**
38+
* @param {number[]} nums
39+
* @param {number} target
40+
* @return {boolean}
41+
*/
42+
var search = function(nums, target) {
43+
var left = 0;
44+
var right = nums.length - 1;
45+
var mid = 0;
46+
while (left <= right) {
47+
mid = Math.floor((left + right) / 2);
48+
if (nums[mid] === target) return true;
49+
if (nums[mid] > nums[left]) {
50+
if (nums[left] <= target && target < nums[mid]) {
51+
right = mid - 1;
52+
} else {
53+
left = mid + 1;
54+
}
55+
} else if (nums[mid] < nums[left]) {
56+
if (nums[mid] < target && target <= nums[right]) {
57+
left = mid + 1;
58+
} else {
59+
right = mid - 1;
60+
}
61+
} else {
62+
left++;
63+
}
64+
}
65+
return false;
66+
};
67+
```
68+
69+
**Explain:**
70+
71+
see [Search in Rotated Sorted Array](./search-in-rotated-sorted-array.html).
72+
73+
1. 判断哪边是有序的
74+
2. 判断 `target` 在有序的那边还是无序的那边
75+
76+
注意重复数字的情况下,只能一个个移动,因为没法判断在哪边。这样算法最坏的情况就是 `O(n)` 了。
77+
78+
**Complexity:**
79+
80+
* Time complexity : O(n).
81+
* Space complexity : O(n).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# 82. Remove Duplicates from Sorted List II
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Linked List.
5+
- Similar Questions: Remove Duplicates from Sorted List.
6+
7+
## Problem
8+
9+
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only **distinct** numbers from the original list.
10+
11+
**Example 1:**
12+
13+
```
14+
Input: 1->2->3->3->4->4->5
15+
Output: 1->2->5
16+
```
17+
18+
**Example 2:**
19+
20+
```
21+
Input: 1->1->1->2->3
22+
Output: 2->3
23+
```
24+
25+
## Solution
26+
27+
```javascript
28+
/**
29+
* Definition for singly-linked list.
30+
* function ListNode(val) {
31+
* this.val = val;
32+
* this.next = null;
33+
* }
34+
*/
35+
/**
36+
* @param {ListNode} head
37+
* @return {ListNode}
38+
*/
39+
var deleteDuplicates = function(head) {
40+
var newHead = new ListNode(0);
41+
var now = newHead;
42+
var tmp = head;
43+
var val = 0;
44+
45+
while (tmp) {
46+
val = tmp.val;
47+
if (tmp.next && tmp.next.val === val) {
48+
tmp = tmp.next;
49+
while (tmp && tmp.val === val) tmp = tmp.next;
50+
} else {
51+
now.next = tmp;
52+
now = tmp;
53+
tmp = tmp.next;
54+
now.next = null;
55+
}
56+
}
57+
58+
return newHead.next;
59+
};
60+
```
61+
62+
**Explain:**
63+
64+
nope.
65+
66+
**Complexity:**
67+
68+
* Time complexity : O(n).
69+
* Space complexity : O(1).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# 83. Remove Duplicates from Sorted List
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Linked List.
5+
- Similar Questions: Remove Duplicates from Sorted List II.
6+
7+
## Problem
8+
9+
Given a sorted linked list, delete all duplicates such that each element appear only **once**.
10+
11+
**Example 1:**
12+
13+
```
14+
Input: 1->1->2
15+
Output: 1->2
16+
```
17+
18+
**Example 2:**
19+
20+
```
21+
Input: 1->1->2->3->3
22+
Output: 1->2->3
23+
```
24+
25+
## Solution
26+
27+
```javascript
28+
/**
29+
* Definition for singly-linked list.
30+
* function ListNode(val) {
31+
* this.val = val;
32+
* this.next = null;
33+
* }
34+
*/
35+
/**
36+
* @param {ListNode} head
37+
* @return {ListNode}
38+
*/
39+
var deleteDuplicates = function(head) {
40+
var now = head;
41+
while (now) {
42+
if (now.next && now.next.val === now.val) {
43+
now.next = now.next.next;
44+
} else {
45+
now = now.next;
46+
}
47+
}
48+
return head;
49+
};
50+
```
51+
52+
**Explain:**
53+
54+
nope.
55+
56+
**Complexity:**
57+
58+
* Time complexity : O(n).
59+
* Space complexity : O(1).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# 84. Largest Rectangle in Histogram
2+
3+
- Difficulty: Hard.
4+
- Related Topics: Array, Stack.
5+
- Similar Questions: Maximal Rectangle.
6+
7+
## Problem
8+
9+
Given **n** non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
10+
11+
![](https://leetcode.com/static/images/problemset/histogram.png)
12+
13+
Above is a histogram where width of each bar is 1, given height = ```[2,1,5,6,2,3]```.
14+
15+
![](https://leetcode.com/static/images/problemset/histogram_area.png)
16+
17+
The largest rectangle is shown in the shaded area, which has area = ```10``` unit.
18+
19+
**Example:**
20+
21+
```
22+
Input: [2,1,5,6,2,3]
23+
Output: 10
24+
```
25+
26+
## Solution
27+
28+
```javascript
29+
/**
30+
* @param {number[]} heights
31+
* @return {number}
32+
*/
33+
var largestRectangleArea = function(heights) {
34+
var len = heights.length;
35+
var stack = [];
36+
var max = 0;
37+
var h = 0;
38+
var w = 0;
39+
40+
for (var i = 0; i <= len; i++) {
41+
while (stack.length && (i === len || heights[i] <= heights[stack[stack.length - 1]])) {
42+
h = heights[stack.pop()];
43+
w = stack.length === 0 ? i : i - stack[stack.length - 1] - 1;
44+
max = Math.max(max, h * w);
45+
}
46+
stack.push(i);
47+
}
48+
49+
return max;
50+
};
51+
```
52+
53+
**Explain:**
54+
55+
nope.
56+
57+
**Complexity:**
58+
59+
* Time complexity : O(n).
60+
* Space complexity : O(n).

0 commit comments

Comments
 (0)