Skip to content

Commit ac3d1b7

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0198
1 parent 6442566 commit ac3d1b7

File tree

6 files changed

+79
-44
lines changed

6 files changed

+79
-44
lines changed

solution/0100-0199/0127.Word Ladder/README_EN.md

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,18 @@
77
<p>Given two words (<em>beginWord</em> and <em>endWord</em>), and a dictionary&#39;s word list, find the length of shortest transformation sequence from <em>beginWord</em> to <em>endWord</em>, such that:</p>
88

99
<ol>
10-
1110
<li>Only one letter can be changed at a time.</li>
12-
1311
<li>Each transformed word must exist in the word list. Note that <em>beginWord</em> is <em>not</em> a transformed word.</li>
14-
1512
</ol>
1613

1714
<p><strong>Note:</strong></p>
1815

1916
<ul>
20-
2117
<li>Return 0 if there is no such transformation sequence.</li>
22-
2318
<li>All words have the same length.</li>
24-
2519
<li>All words contain only lowercase alphabetic characters.</li>
26-
2720
<li>You may assume no duplicates in the word list.</li>
28-
2921
<li>You may assume <em>beginWord</em> and <em>endWord</em> are non-empty and are not the same.</li>
30-
3122
</ul>
3223

3324
<p><strong>Example 1:</strong></p>

solution/0100-0199/0139.Word Break/README_EN.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@
99
<p><strong>Note:</strong></p>
1010

1111
<ul>
12-
1312
<li>The same word in the dictionary may be reused multiple times in the segmentation.</li>
14-
1513
<li>You may assume the dictionary does not contain duplicate words.</li>
16-
1714
</ul>
1815

1916
<p><strong>Example 1:</strong></p>

solution/0100-0199/0198.House Robber/README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,44 @@
3535
<!-- 这里可写当前语言的特殊实现逻辑 -->
3636

3737
```python
38-
38+
class Solution:
39+
def rob(self, nums: List[int]) -> int:
40+
if not nums:
41+
return 0
42+
n = len(nums)
43+
if n == 1:
44+
return nums[0]
45+
dp = [0 for _ in range(n)]
46+
dp[0] = nums[0]
47+
dp[1] = max(nums[0], nums[1])
48+
for i in range(2, n):
49+
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])
50+
return dp[n - 1]
3951
```
4052

4153
### **Java**
4254

4355
<!-- 这里可写当前语言的特殊实现逻辑 -->
4456

4557
```java
46-
58+
class Solution {
59+
public int rob(int[] nums) {
60+
int n;
61+
if (nums == null || (n = nums.length) == 0) {
62+
return 0;
63+
}
64+
if (n == 1) {
65+
return nums[0];
66+
}
67+
int[] dp = new int[n];
68+
dp[0] = nums[0];
69+
dp[1] = Math.max(nums[0], nums[1]);
70+
for (int i = 2; i < n; ++i) {
71+
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
72+
}
73+
return dp[n - 1];
74+
}
75+
}
4776
```
4877

4978
### **...**

solution/0100-0199/0198.House Robber/README_EN.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,42 @@
4141
### **Python3**
4242

4343
```python
44-
44+
class Solution:
45+
def rob(self, nums: List[int]) -> int:
46+
if not nums:
47+
return 0
48+
n = len(nums)
49+
if n == 1:
50+
return nums[0]
51+
dp = [0 for _ in range(n)]
52+
dp[0] = nums[0]
53+
dp[1] = max(nums[0], nums[1])
54+
for i in range(2, n):
55+
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])
56+
return dp[n - 1]
4557
```
4658

4759
### **Java**
4860

4961
```java
50-
62+
class Solution {
63+
public int rob(int[] nums) {
64+
int n;
65+
if (nums == null || (n = nums.length) == 0) {
66+
return 0;
67+
}
68+
if (n == 1) {
69+
return nums[0];
70+
}
71+
int[] dp = new int[n];
72+
dp[0] = nums[0];
73+
dp[1] = Math.max(nums[0], nums[1]);
74+
for (int i = 2; i < n; ++i) {
75+
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
76+
}
77+
return dp[n - 1];
78+
}
79+
}
5180
```
5281

5382
### **...**
Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
class Solution {
22
public int rob(int[] nums) {
3-
if (nums == null || nums.length == 0) {
3+
int n;
4+
if (nums == null || (n = nums.length) == 0) {
45
return 0;
56
}
6-
7-
int n = nums.length;
87
if (n == 1) {
98
return nums[0];
109
}
11-
12-
int[] result = new int[n];
13-
result[0] = nums[0];
14-
result[1] = Math.max(nums[0], nums[1]);
15-
10+
int[] dp = new int[n];
11+
dp[0] = nums[0];
12+
dp[1] = Math.max(nums[0], nums[1]);
1613
for (int i = 2; i < n; ++i) {
17-
result[i] = Math.max(nums[i] + result[i - 2], result[i - 1]);
14+
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
1815
}
19-
20-
return result[n - 1];
21-
16+
return dp[n - 1];
2217
}
2318
}
Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
class Solution:
2-
def rob(self, nums):
3-
"""
4-
:type nums: List[int]
5-
:rtype: int
6-
"""
7-
length=len(nums)
8-
if length == 0:
2+
def rob(self, nums: List[int]) -> int:
3+
if not nums:
94
return 0
10-
if length == 1:
5+
n = len(nums)
6+
if n == 1:
117
return nums[0]
12-
res=[0]*length
13-
res[0]=nums[0]
14-
res[1]=max(nums[0],nums[1])
15-
16-
for i in range(2,length):
17-
res[i]=max(nums[i]+res[i-2],res[i-1])
18-
19-
return res[-1]
8+
dp = [0 for _ in range(n)]
9+
dp[0] = nums[0]
10+
dp[1] = max(nums[0], nums[1])
11+
for i in range(2, n):
12+
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])
13+
return dp[n - 1]

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy