Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5bb22d2

Browse files
authored
Added tasks 198-200.
1 parent 01b0e4e commit 5bb22d2

File tree

6 files changed

+166
-0
lines changed

6 files changed

+166
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0101_0200.s0198_house_robber;
2+
3+
public class Solution {
4+
public int rob(int[] nums) {
5+
if (nums.length == 0) {
6+
return 0;
7+
}
8+
if (nums.length == 1) {
9+
return nums[0];
10+
}
11+
if (nums.length == 2) {
12+
return Math.max(nums[0], nums[1]);
13+
}
14+
int[] profit = new int[nums.length];
15+
profit[0] = nums[0];
16+
profit[1] = Math.max(nums[1], nums[0]);
17+
for (int i = 2; i < nums.length; i++) {
18+
profit[i] = Math.max(profit[i - 1], nums[i] + profit[i - 2]);
19+
}
20+
return profit[nums.length - 1];
21+
}
22+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g0101_0200.s0199_binary_tree_right_side_view;
2+
3+
import com_github_leetcode.TreeNode;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
/*
8+
* Definition for a binary tree node.
9+
* public class TreeNode {
10+
* int val;
11+
* TreeNode left;
12+
* TreeNode right;
13+
* TreeNode() {}
14+
* TreeNode(int val) { this.val = val; }
15+
* TreeNode(int val, TreeNode left, TreeNode right) {
16+
* this.val = val;
17+
* this.left = left;
18+
* this.right = right;
19+
* }
20+
* }
21+
*/
22+
public class Solution {
23+
public List<Integer> rightSideView(TreeNode root) {
24+
List<Integer> list = new ArrayList<>();
25+
recurse(root, 0, list);
26+
return list;
27+
}
28+
29+
private void recurse(TreeNode node, int level, List<Integer> list) {
30+
if (node != null) {
31+
if (list.size() < level + 1) {
32+
list.add(node.val);
33+
}
34+
recurse(node.right, level + 1, list);
35+
recurse(node.left, level + 1, list);
36+
}
37+
}
38+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0101_0200.s0200_number_of_islands;
2+
3+
public class Solution {
4+
public int numIslands(char[][] grid) {
5+
int islands = 0;
6+
if (grid != null && grid.length != 0 && grid[0].length != 0) {
7+
for (int i = 0; i < grid.length; i++) {
8+
for (int j = 0; j < grid[0].length; j++) {
9+
if (grid[i][j] == '1') {
10+
dfs(grid, i, j);
11+
islands++;
12+
}
13+
}
14+
}
15+
}
16+
return islands;
17+
}
18+
19+
private void dfs(char[][] grid, int x, int y) {
20+
if (x < 0 || grid.length <= x || y < 0 || grid[0].length <= y || grid[x][y] != '1') {
21+
return;
22+
}
23+
grid[x][y] = 'x';
24+
dfs(grid, x + 1, y);
25+
dfs(grid, x - 1, y);
26+
dfs(grid, x, y + 1);
27+
dfs(grid, x, y - 1);
28+
}
29+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0101_0200.s0198_house_robber;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
public class SolutionTest {
9+
@Test
10+
public void rob() {
11+
assertThat(new Solution().rob(new int[] {1, 2, 3, 1}), equalTo(4));
12+
}
13+
14+
@Test
15+
public void rob2() {
16+
assertThat(new Solution().rob(new int[] {2, 7, 9, 3, 1}), equalTo(12));
17+
}
18+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g0101_0200.s0199_binary_tree_right_side_view;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import com_github_leetcode.TreeNode;
7+
import java.util.Arrays;
8+
import org.junit.Test;
9+
10+
public class SolutionTest {
11+
@Test
12+
public void rightSideView() {
13+
TreeNode left = new TreeNode(2, null, new TreeNode(5));
14+
TreeNode right = new TreeNode(3, null, new TreeNode(4));
15+
TreeNode root = new TreeNode(1, left, right);
16+
assertThat(new Solution().rightSideView(root), equalTo(Arrays.asList(1, 3, 4)));
17+
}
18+
19+
@Test
20+
public void rightSideView2() {
21+
TreeNode root = new TreeNode(1, null, new TreeNode(3));
22+
assertThat(new Solution().rightSideView(root), equalTo(Arrays.asList(1, 3)));
23+
}
24+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g0101_0200.s0200_number_of_islands;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
public class SolutionTest {
9+
@Test
10+
public void numIslands() {
11+
assertThat(
12+
new Solution()
13+
.numIslands(
14+
new char[][] {
15+
{'1', '1', '1', '1', '0'},
16+
{'1', '1', '0', '1', '0'},
17+
{'1', '1', '0', '0', '0'},
18+
{'0', '0', '0', '0', '0'}
19+
}),
20+
equalTo(1));
21+
}
22+
23+
public void numIslands2() {
24+
assertThat(
25+
new Solution()
26+
.numIslands(
27+
new char[][] {
28+
{'1', '1', '0', '0', '0'},
29+
{'1', '1', '0', '0', '0'},
30+
{'0', '0', '1', '0', '0'},
31+
{'0', '0', '0', '1', '1'}
32+
}),
33+
equalTo(3));
34+
}
35+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /