Skip to content

Commit 7268b76

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 664f22d + 351e142 commit 7268b76

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed

src/main/java/com/fishercoder/solutions/_29.java

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
11
package com.fishercoder.solutions;
2+
import java.util.HashMap;
3+
import java.util.Map;
4+
25

36
public class _29 {
47

58
public static class Solution1 {
9+
10+
private static Map<String, Boolean> branchCoverage = new HashMap<>();
11+
12+
static {
13+
branchCoverage.put("flag1", false);
14+
branchCoverage.put("flag2", false);
15+
branchCoverage.put("flag3", false);
16+
branchCoverage.put("flag4", false);
17+
branchCoverage.put("flag5", false);
18+
branchCoverage.put("flag6", false);
19+
branchCoverage.put("flag7", false);
20+
}
21+
22+
public void printCoverage() {
23+
for (Map.Entry<String, Boolean> entry : branchCoverage.entrySet()) {
24+
System.out.println(entry.getKey() + " was " + (entry.getValue() ? "hit" : "not hit"));
25+
}
26+
}
27+
628
/**
729
* credit: https://leetcode.com/problems/divide-two-integers/solution/ solution 1
830
* <p>
@@ -16,29 +38,37 @@ public static class Solution1 {
1638
* Space: O(1)
1739
*/
1840
public int divide(int dividend, int divisor) {
19-
if (dividend == Integer.MIN_VALUE && divisor == -1) {
41+
if (dividend == Integer.MIN_VALUE && divisor == -1) { //1
42+
branchCoverage.put("flag1", true);
2043
return Integer.MAX_VALUE;
2144
}
2245
int negativeCount = 0;
23-
if (dividend < 0) {
46+
if (dividend < 0) { //2
47+
branchCoverage.put("flag2", true);
2448
negativeCount++;
25-
} else {
49+
} else { //3
50+
branchCoverage.put("flag3", true);
2651
dividend = -dividend;
2752
}
28-
if (divisor < 0) {
53+
if (divisor < 0) { //4
54+
branchCoverage.put("flag4", true);
2955
negativeCount++;
30-
} else {
56+
} else { //5
57+
branchCoverage.put("flag5", true);
3158
divisor = -divisor;
3259
}
3360

3461
int quotient = 0;
35-
while (dividend <= divisor) {
62+
while (dividend <= divisor) { //6
63+
branchCoverage.put("flag6", true);
3664
dividend -= divisor;
3765
quotient++;
3866
}
39-
if (negativeCount == 1) {
67+
if (negativeCount == 1) { //7
68+
branchCoverage.put("flag7", true);
4069
quotient = -quotient;
4170
}
71+
printCoverage();
4272
return quotient;
4373
}
4474
}

src/main/java/com/fishercoder/solutions/_487.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
package com.fishercoder.solutions;
2+
import java.util.HashMap;
3+
import java.util.Map;
4+
25

36
public class _487 {
47

@@ -35,25 +38,45 @@ public static class Solution2 {
3538
/**
3639
* This is a more generic solution adapted from https://leetcode.com/problems/max-consecutive-ones-iii/, just set k = 1 here.
3740
*/
38-
public static int findMaxConsecutiveOnes(int[] nums) {
41+
42+
private static Map<String, Boolean> branchCoverage = new HashMap<>();
43+
static {
44+
branchCoverage.put("flag1", false);
45+
branchCoverage.put("flag2", false);
46+
branchCoverage.put("flag3", false);
47+
branchCoverage.put("flag4", false);
48+
}
49+
50+
public int findMaxConsecutiveOnes(int[] nums) {
3951
int len = nums.length;
4052
int left = 0;
4153
int right = 0;
4254
int ans = 0;
4355
int k = 1;
4456
for (; right < len; right++) {
57+
branchCoverage.put("flag1", true);
4558
if (nums[right] == 0) {
59+
branchCoverage.put("flag2", true);
4660
k--;
4761
}
4862
while (k < 0) {
63+
branchCoverage.put("flag3", true);
4964
if (nums[left] == 0) {
65+
branchCoverage.put("flag4", true);
5066
k++;
5167
}
5268
left++;
5369
}
5470
ans = Math.max(ans, right - left + 1);
5571
}
72+
printCoverage();
5673
return ans;
5774
}
75+
76+
public void printCoverage() {
77+
for (Map.Entry<String, Boolean> entry : branchCoverage.entrySet()) {
78+
System.out.println(entry.getKey() + " was " + (entry.getValue() ? "hit" : "not hit"));
79+
}
80+
}
5881
}
5982
}

0 commit comments

Comments
 (0)