Skip to content

Commit 0232d05

Browse files
committed
change js to md
1 parent 1acf84d commit 0232d05

6 files changed

+502
-0
lines changed
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# 32. Longest Valid Parentheses
2+
3+
- Difficulty: Hard.
4+
- Related Topics: String, Dynamic Programming.
5+
- Similar Questions: Valid Parentheses.
6+
7+
## Problem
8+
9+
Given a string containing just the characters ```'('``` and ```')'```, find the length of the longest valid (well-formed) parentheses substring.
10+
11+
**Example 1:**
12+
13+
```
14+
Input: "(()"
15+
Output: 2
16+
Explanation: The longest valid parentheses substring is "()"
17+
```
18+
19+
**Example 2:**
20+
21+
```
22+
Input: ")()())"
23+
Output: 4
24+
Explanation: The longest valid parentheses substring is "()()"
25+
```
26+
27+
## Solution
28+
29+
```javascript
30+
/**
31+
* @param {string} s
32+
* @return {number}
33+
*/
34+
var longestValidParentheses = function(s) {
35+
var max = 0;
36+
var len = s.length;
37+
var dp = Array(len).fill(0);
38+
var tmp = 0;
39+
var getNum = function (index) {
40+
return index >= 0 ? dp[index] : 0;
41+
};
42+
43+
for (var i = 1; i < len; i++) {
44+
if (s[i] === ')') {
45+
if (s[i - 1] === '(') {
46+
dp[i] = getNum(i - 2) + 2;
47+
} else {
48+
tmp = i - dp[i - 1] - 1;
49+
if (tmp >= 0 && s[tmp] === '(') {
50+
dp[i] = dp[i - 1] + getNum(tmp - 1) + 2;
51+
}
52+
}
53+
max = Math.max(max, dp[i]);
54+
}
55+
}
56+
57+
return max;
58+
};
59+
```
60+
61+
**Explain:**
62+
63+
动态规划
64+
1. 建立等长的 dp 数组,填充 0,每个数值代表 s 相应位置处的连续匹配括号的长度
65+
2. '(' 不影响结果,不处理,数值为 0
66+
3. ')' 的上一个是 '(' 的话,配对,数值为上一数值 + 2
67+
4. ')' 的上一个是 ')' 时,这时候要看上一个数值,上一个数值代表连续匹配的括号数量,记为 n
68+
如果回退 n + 1 个,正好是个 '(' 的话,配对,数值为上一数值 + (回退 n + 2 个的数值) + 2
69+
否则是不匹配,不处理,数值为 0
70+
71+
**Complexity:**
72+
73+
* Time complexity : O(n).
74+
* Space complexity : O(n).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# 33. Search in Rotated Sorted Array
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Binary Search.
5+
- Similar Questions: Search in Rotated Sorted Array II, Find Minimum 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,1,2,4,5,6,7]``` might become ```[4,5,6,7,0,1,2]```).
12+
13+
You are given a target value to search. If found in the array return its index, otherwise return ```-1```.
14+
15+
You may assume no duplicate exists in the array.
16+
17+
Your algorithm's runtime complexity must be in the order of **O**(log **n**).
18+
19+
**Example 1:**
20+
21+
```
22+
Input: nums = [4,5,6,7,0,1,2], target = 0
23+
Output: 4
24+
```
25+
26+
**Example 2:**
27+
28+
```
29+
Input: nums = [4,5,6,7,0,1,2], target = 3
30+
Output: -1
31+
```
32+
33+
## Solution
34+
35+
```javascript
36+
/**
37+
* @param {number[]} nums
38+
* @param {number} target
39+
* @return {number}
40+
*/
41+
var search = function(nums, target) {
42+
var len = nums.length
43+
var left = 0;
44+
var right = len - 1;
45+
var mid = 0;
46+
47+
while (left <= right) {
48+
mid = left + Math.floor((right - left) / 2);
49+
if (nums[mid] === target) return mid;
50+
if (nums[mid] > nums[right]) {
51+
if (nums[left] <= target && target < nums[mid]) {
52+
right = mid - 1;
53+
} else {
54+
left = mid + 1;
55+
}
56+
} else {
57+
if (nums[mid] < target && target <= nums[right]) {
58+
left = mid + 1;
59+
} else {
60+
right = mid - 1;
61+
}
62+
}
63+
}
64+
65+
return -1;
66+
};
67+
```
68+
69+
**Explain:**
70+
71+
题意:
72+
73+
输入数组是已排序的数组在某个点上翻转了一下,比如 01234 在 2 上翻转,变成 23401,在输入数组里找是否存在某个数
74+
75+
解:二分查找
76+
77+
(规律:输入数组中取任意一段,从中间分开,必定有一边是已排序的)
78+
79+
先通过中间值与末尾值比大小,判断已排序的是左边还是右边,当然也可以通过中间值跟起点值比大小来判断
80+
81+
(目标值等于中间值的话,就结束了,其实也包括二分查找的极限情况,区间里只剩一个值)
82+
83+
然后判断目标值是否在已排序的那边,在的话在这边继续查找,否则去另一半继续查找
84+
85+
**Complexity:**
86+
87+
* Time complexity : O(log(n)).
88+
* Space complexity : O(1).

001-100/34. Search for a Range.md

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# 34. Search for a Range
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Binary Search.
5+
- Similar Questions: First Bad Version.
6+
7+
## Problem
8+
9+
Given an array of integers ```nums``` sorted in ascending order, find the starting and ending position of a given ```target``` value.
10+
11+
Your algorithm's runtime complexity must be in the order of **O**(log **n**).
12+
13+
If the target is not found in the array, return ```[-1, -1]```.
14+
15+
**Example 1:**
16+
17+
```
18+
Input: nums = [5,7,7,8,8,10], target = 8
19+
Output: [3,4]
20+
```
21+
22+
**Example 2:**
23+
24+
```
25+
Input: nums = [5,7,7,8,8,10], target = 6
26+
Output: [-1,-1]
27+
```
28+
29+
## Solution
30+
31+
```javascript
32+
/**
33+
* @param {number[]} nums
34+
* @param {number} target
35+
* @return {number[]}
36+
*/
37+
var searchRange = function(nums, target) {
38+
var res = [-1, -1];
39+
var left = find(nums, target, true);
40+
var right = find(nums, target, false);
41+
if (!nums.length) return res;
42+
if (left > right) return res;
43+
return [left, right];
44+
};
45+
46+
var find = function (nums, target, findLeft) {
47+
var left = 0;
48+
var right = nums.length - 1;
49+
var mid = 0;
50+
51+
while (left <= right) {
52+
mid = Math.floor((left + right) / 2);
53+
if (nums[mid] > target || (findLeft && nums[mid] === target)) {
54+
right = mid - 1;
55+
} else {
56+
left = mid + 1;
57+
}
58+
}
59+
60+
return findLeft ? left : right;
61+
};
62+
```
63+
64+
**Explain:**
65+
66+
二分查找:
67+
68+
分两次,第一次找左边位置,第二次找右边位置
69+
70+
1. 中间值小于目标,继续去右半部分找
71+
2. 中间值大于目标,继续去左半部分找
72+
3. 中间值等于目标,找左位置时,继续去左半部分找;
73+
找右位置时,继续去右半部分找。
74+
4. 最终不能找到的话,左位置是会大于右位置的,否则代表找到
75+
76+
**Complexity:**
77+
78+
* Time complexity : O(log(n)).
79+
* Space complexity : O(1).

001-100/35. Search Insert Position.md

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# 35. Search Insert Position
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Array, Binary Search.
5+
- Similar Questions: First Bad Version.
6+
7+
## Problem
8+
9+
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
10+
11+
You may assume no duplicates in the array.
12+
13+
**Example 1:**
14+
15+
```
16+
Input: [1,3,5,6], 5
17+
Output: 2
18+
```
19+
20+
**Example 2:**
21+
22+
```
23+
Input: [1,3,5,6], 2
24+
Output: 1
25+
```
26+
27+
**Example 3:**
28+
29+
```
30+
Input: [1,3,5,6], 7
31+
Output: 4
32+
```
33+
34+
**Example 4:**
35+
36+
```
37+
Input: [1,3,5,6], 0
38+
Output: 0
39+
```
40+
41+
## Solution
42+
43+
```javascript
44+
/**
45+
* @param {number[]} nums
46+
* @param {number} target
47+
* @return {number}
48+
*/
49+
var searchInsert = function(nums, target) {
50+
var len = nums.length;
51+
var left = 0;
52+
var right = len - 1;
53+
var mid = 0;
54+
55+
if (!len) return 0;
56+
57+
while (left <= right) {
58+
mid = Math.floor((left + right) / 2);
59+
if (nums[mid] > target) {
60+
right = mid - 1;
61+
} else if (nums[mid] < target) {
62+
left = mid + 1;
63+
} else {
64+
return mid;
65+
}
66+
}
67+
68+
return (nums[mid] > target) ? mid : (mid + 1);
69+
};
70+
```
71+
72+
**Explain:**
73+
74+
nope.
75+
76+
**Complexity:**
77+
78+
* Time complexity : O(log(n)).
79+
* Space complexity : O(1).

0 commit comments

Comments
 (0)