Skip to content

Commit 14c9dba

Browse files
committed
add 1027
1 parent 86ece88 commit 14c9dba

17 files changed

+3952
-3228
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ node script deploy.sh
6363

6464
## 🔐 Problems & Solutions
6565

66-
完成进度([1141](./TOC-By-ID.md)🔑/ [2813](https://leetcode.cn/problemset/all/)🔒)
66+
完成进度([1142](./TOC-By-ID.md)🔑/ [2975](https://leetcode.cn/problemset/all/)🔒)
6767

6868
- 🔗 [标签查找](./TOC-By-Tag.md)
6969

TOC-By-ID.md

+957-950
Large diffs are not rendered by default.

TOC-By-Tag.md

+2,102-2,082
Large diffs are not rendered by default.

algorithms/1-100/26.remove-duplicates-from-sorted-array.md

-35
This file was deleted.

algorithms/1-100/4.median-of-two-sorted-arrays.md

-99
This file was deleted.

algorithms/1001-1100/1027.longest-arithmetic-subsequence.md

-54
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# [1027.最长等差数列](https://leetcode.cn/problems/longest-arithmetic-subsequence/)
2+
3+
<p>给你一个整数数组&nbsp;<code>nums</code>,返回 <code>nums</code>&nbsp;中最长等差子序列的<strong>长度</strong>。</p>
4+
5+
<p>回想一下,<code>nums</code> 的子序列是一个列表&nbsp;<code>nums[i<sub>1</sub>], nums[i<sub>2</sub>], ..., nums[i<sub>k</sub>]</code> ,且&nbsp;<code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k</sub> &lt;= nums.length - 1</code>。并且如果&nbsp;<code>seq[i+1] - seq[i]</code>(&nbsp;<code>0 &lt;= i &lt; seq.length - 1</code>) 的值都相同,那么序列&nbsp;<code>seq</code>&nbsp;是等差的。</p>
6+
7+
<p>&nbsp;</p>
8+
9+
<p><strong>示例 1:</strong></p>
10+
11+
<pre>
12+
<strong>输入:</strong>nums = [3,6,9,12]
13+
<strong>输出:</strong>4
14+
<strong>解释: </strong>
15+
整个数组是公差为 3 的等差数列。
16+
</pre>
17+
18+
<p><strong>示例 2:</strong></p>
19+
20+
<pre>
21+
<strong>输入:</strong>nums = [9,4,7,2,10]
22+
<strong>输出:</strong>3
23+
<strong>解释:</strong>
24+
最长的等差子序列是 [4,7,10]。
25+
</pre>
26+
27+
<p><strong>示例 3:</strong></p>
28+
29+
<pre>
30+
<strong>输入:</strong>nums = [20,1,15,3,10,5,8]
31+
<strong>输出:</strong>4
32+
<strong>解释:</strong>
33+
最长的等差子序列是 [20,15,10,5]。
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
38+
<p><strong>提示:</strong></p>
39+
40+
<ul>
41+
<li><code>2 &lt;= nums.length &lt;= 1000</code></li>
42+
<li><code>0 &lt;= nums[i] &lt;= 500</code></li>
43+
</ul>
44+
45+
46+
<details>
47+
<summary>标签:</summary>
48+
['数组', '哈希表', '二分查找', '动态规划']
49+
</details>
50+
51+
<details>
52+
<summary>难度:Medium</summary>
53+
喜欢:280
54+
</details>
55+
56+
57+
----------
58+
59+
# 算法1
60+
61+
## 算法思路
62+
63+
64+
65+
动态规划,每个位置保存截止当前位置,该位置与之前所有元素的差以及数列长度
66+
67+
```javascript
68+
69+
```
70+
71+
## 复杂度分析
72+
73+
时间复杂度:$O(n^2)$
74+
75+
空间复杂度:$O(1)$
76+
77+
## 代码实现
78+
79+
```cpp []
80+
81+
```
82+
83+
```java []
84+
class Solution {
85+
public int longestArithSeqLength(int[] nums) {
86+
int n = nums.length;
87+
int[][] dp = new int[n][20001];
88+
int res = 0;
89+
for(int i=0; i<n; i++) {
90+
for(int j=0; j<i; j++) {
91+
int diff = nums[i] - nums[j] + 10000;
92+
dp[i][diff] = Math.max(dp[i][diff], dp[j][diff] + 1);
93+
res = Math.max(res, dp[i][diff]);
94+
}
95+
}
96+
return res + 1;
97+
}
98+
}
99+
```
100+
101+
```javascript
102+
var longestArithSeqLength = function (A) {
103+
if (A.length <= 1) return A.length;
104+
let dp = new Array(A.length);
105+
let max = 0;
106+
dp[0] = {};
107+
for (let i = 1; i < A.length; i++) {
108+
dp[i] = {};
109+
for (let j = 0; j < i; j++) {
110+
if (dp[j][A[i] - A[j]]) {
111+
dp[i][A[i] - A[j]] = dp[j][A[i] - A[j]] + 1;
112+
} else {
113+
dp[i][A[i] - A[j]] = 2;
114+
}
115+
max = Math.max(max, dp[i][A[i] - A[j]]);
116+
}
117+
}
118+
return max;
119+
};
120+
```
121+
122+
123+
124+
## 参考文献
125+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# [1108.IP 地址无效化](https://leetcode.cn/problems/defanging-an-ip-address/)
2+
3+
<p>给你一个有效的 <a href="https://baike.baidu.com/item/IPv4" target="_blank">IPv4</a> 地址&nbsp;<code>address</code>,返回这个 IP 地址的无效化版本。</p>
4+
5+
<p>所谓无效化&nbsp;IP 地址,其实就是用&nbsp;<code>&quot;[.]&quot;</code>&nbsp;代替了每个 <code>&quot;.&quot;</code>。</p>
6+
7+
<p>&nbsp;</p>
8+
9+
<p><strong>示例 1:</strong></p>
10+
11+
<pre><strong>输入:</strong>address = &quot;1.1.1.1&quot;
12+
<strong>输出:</strong>&quot;1[.]1[.]1[.]1&quot;
13+
</pre>
14+
15+
<p><strong>示例 2:</strong></p>
16+
17+
<pre><strong>输入:</strong>address = &quot;255.100.50.0&quot;
18+
<strong>输出:</strong>&quot;255[.]100[.]50[.]0&quot;
19+
</pre>
20+
21+
<p>&nbsp;</p>
22+
23+
<p><strong>提示:</strong></p>
24+
25+
<ul>
26+
<li>给出的&nbsp;<code>address</code>&nbsp;是一个有效的 IPv4 地址</li>
27+
</ul>
28+
29+
30+
<details>
31+
<summary>标签:</summary>
32+
['字符串']
33+
</details>
34+
35+
<details>
36+
<summary>难度:Easy</summary>
37+
喜欢:126
38+
</details>
39+
40+
41+
----------
42+
43+
# 算法1
44+
45+
## 算法思路
46+
47+
blablabla
48+
49+
## 复杂度分析
50+
51+
时间复杂度:$O(n^2)$
52+
53+
空间复杂度:$O(1)$
54+
55+
## 代码实现
56+
57+
```cpp []
58+
59+
```
60+
61+
```java []
62+
63+
```
64+
65+
## 参考文献
66+
67+

0 commit comments

Comments
 (0)