Skip to content

Commit dc72d0a

Browse files
committed
Modified 2 solutions
1 parent d596729 commit dc72d0a

File tree

2 files changed

+28
-61
lines changed

2 files changed

+28
-61
lines changed

Easy/Paint Fence.java

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
11
class Solution {
2-
public int numWays(int n, int k) {
3-
if (n == 0) {
4-
return 0;
5-
}
6-
7-
if (n == 1) {
8-
return k;
9-
}
10-
11-
if (n == 2) {
12-
return k * k;
13-
}
14-
15-
int[] ways = new int[n + 1];
16-
ways[0] = 0;
17-
ways[1] = k;
18-
ways[2] = k * k;
19-
20-
for (int i = 3; i <= n; i++) {
21-
ways[i] = ways[i - 1] * (k - 1) + ways[i - 2] * (k - 1);
22-
}
23-
24-
return ways[n];
2+
public int numWays(int n, int k) {
3+
int[] ways = new int[Math.max(n + 1, 3)];
4+
ways[0] = 0;
5+
ways[1] = k;
6+
ways[2] = k * k;
7+
for (int i = 3; i <= n; i++) {
8+
ways[i] = (ways[i - 1] * (k - 1)) + (ways[i - 2] * (k - 1));
259
}
10+
return ways[n];
11+
}
2612
}
Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,22 @@
11
class Solution {
2-
3-
public List<List<String>> findDuplicate(String[] paths) {
4-
5-
Map<String, ArrayList<String>> map = new HashMap<>();
6-
List<List<String>> ans = new ArrayList<>();
7-
8-
for (String s : paths) {
9-
String[] temp = s.split("\\s");
10-
11-
for (int i=1;i<temp.length;i++) {
12-
String message = temp[i].substring(temp[i].indexOf("(")+1, temp[i].indexOf(")"));
13-
String fileName = temp[i].substring(0, temp[i].indexOf("("));
14-
15-
if (map.containsKey(message)) {
16-
ArrayList<String> tempList = map.get(message);
17-
tempList.add(temp[0] + "/" + fileName);
18-
map.put(message, tempList);
19-
}
20-
else {
21-
ArrayList<String> tempList = new ArrayList<>();
22-
tempList.add(temp[0] + "/" + fileName);
23-
map.put(message, tempList);
24-
}
25-
26-
}
27-
}
28-
29-
for (Map.Entry<String,ArrayList<String>> entry : map.entrySet()) {
30-
31-
List<String> l = entry.getValue();
32-
33-
if (l.size() > 1) {
34-
ans.add(l);
35-
}
36-
37-
}
38-
39-
return ans;
2+
public List<List<String>> findDuplicate(String[] paths) {
3+
Map<String, List<String>> map = new HashMap<>();
4+
for (String path : paths) {
5+
String[] strs = path.split("\\s+");
6+
String filePath = strs[0];
7+
for (int i = 1; i < strs.length; i++) {
8+
int startIdx = strs[i].indexOf('(');
9+
String fileName = strs[i].substring(0, startIdx);
10+
String content = strs[i].substring(startIdx, strs[i].length());
11+
map.computeIfAbsent(content, k -> new ArrayList<>()).add(filePath + "/" + fileName);
12+
}
4013
}
14+
List<List<String>> duplicateFiles = new ArrayList<>();
15+
for (String key : map.keySet()) {
16+
if (map.get(key).size() > 1) {
17+
duplicateFiles.add(map.get(key));
18+
}
19+
}
20+
return duplicateFiles;
21+
}
4122
}

0 commit comments

Comments
 (0)