Skip to content

Commit 340c491

Browse files
committed
Solved 3 problems
1 parent d7d8cd5 commit 340c491

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

Easy/Flood Fill.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
3+
int color = image[sr][sc];
4+
if (color != newColor) {
5+
dfs(image, sr, sc, color, newColor);
6+
}
7+
8+
return image;
9+
}
10+
11+
public void dfs(int[][] image, int sr, int sc, int color, int newColor) {
12+
if (image[sr][sc] == color) {
13+
image[sr][sc] = newColor;
14+
if (sr >= 1) dfs(image, sr-1, sc, color, newColor);
15+
if (sc >= 1) dfs(image, sr, sc-1, color, newColor);
16+
if (sr+1 < image.length) dfs(image, sr+1, sc, color, newColor);
17+
if (sc+1 < image[0].length) dfs(image, sr, sc+1, color, newColor);
18+
}
19+
}
20+
}

Easy/Longest Common Prefix.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public String longestCommonPrefix(String[] strs) {
3+
if (strs == null || strs.length == 0) return "";
4+
5+
String ans = strs[0];
6+
7+
for (int i=1;i<strs.length;i++) {
8+
while(strs[i].indexOf(ans) != 0) {
9+
ans = ans.substring(0,ans.length()-1);
10+
}
11+
}
12+
13+
return ans;
14+
}
15+
}

Easy/Repeated Substring Pattern.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
public boolean repeatedSubstringPattern(String s) {
3+
String s1 = s + s;
4+
String s2 = s1.substring(1,s1.length()-1);
5+
6+
return s2.contains(s);
7+
}
8+
}

0 commit comments

Comments
 (0)