Skip to content

Commit 8a40e9b

Browse files
author
tonardo2015
committed
include solution for leetcode 200 and 26
1 parent 1d4efc2 commit 8a40e9b

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

java/200_number_of_island.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public int numIslands(char[][] grid){
3+
4+
if(grid == null || grid.length == 0){
5+
return 0;
6+
}
7+
8+
int rows = grid.length;
9+
int cols = grid[0].length;
10+
int islandCounter = 0;
11+
for(int r = 0; r < rows; r++){
12+
for(int c = 0; c < cols; c++){
13+
if(grid[r][c] == '1' ){
14+
islandCounter += 1;
15+
dfs(grid, r, c);
16+
}
17+
}
18+
}
19+
return islandCounter;
20+
}
21+
22+
private void dfs(char[][] grid, int r, int c){
23+
if(r < 0 || r >= grid.length || c < 0 || c >= grid[0].length || grid[r][c] == '0'){
24+
return;
25+
}
26+
27+
grid[r][c] = '0';
28+
dfs(grid, r+1, c);
29+
dfs(grid, r-1, c);
30+
dfs(grid, r, c+1);
31+
dfs(grid, r, c-1);
32+
}
33+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class Solution{
2+
public int removeDuplicates(int[] nums){
3+
if(nums.length == 0){
4+
return 0;
5+
}
6+
int uniqNum = 1;
7+
for(int i = 1; i < nums.length; i++){
8+
if(nums[i] != nums[i-1]){
9+
nums[uniqNum] = nums[i];
10+
uniqNum++;
11+
}
12+
}
13+
return uniqNum;
14+
}
15+
16+
public static void main(String[] args){
17+
int[] nums = {0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
18+
Solution sol = new Solution();
19+
System.out.println(sol.removeDuplicates(nums));
20+
}
21+
}

0 commit comments

Comments
 (0)