Skip to content

Commit b618289

Browse files
committed
Modified 2 solutions
1 parent 3e92511 commit b618289

File tree

2 files changed

+23
-27
lines changed

2 files changed

+23
-27
lines changed

Easy/Bulls and Cows.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
class Solution {
22
public String getHint(String secret, String guess) {
33
int[] counter = new int[10];
4-
int cows = 0;
4+
for (Character c : secret.toCharArray()) {
5+
counter[Character.getNumericValue(c)]++;
6+
}
57
int bulls = 0;
8+
int cows = 0;
69
for (int i = 0; i < guess.length(); i++) {
7-
int s = Character.getNumericValue(secret.charAt(i));
8-
int g = Character.getNumericValue(guess.charAt(i));
9-
if (s == g) {
10+
if (guess.charAt(i) == secret.charAt(i)) {
1011
bulls++;
1112
}
12-
else {
13-
if (counter[s] < 0) {
14-
cows++;
15-
}
16-
if (counter[g] > 0) {
17-
cows++;
18-
}
19-
counter[s]++;
20-
counter[g]--;
13+
if (counter[Character.getNumericValue(guess.charAt(i))] > 0) {
14+
counter[Character.getNumericValue(guess.charAt(i))]--;
15+
cows++;
2116
}
2217
}
23-
return new StringBuilder().append(bulls).append("A").append(cows).append("B").toString();
18+
return bulls + "A" + (cows - bulls) + "B";
2419
}
2520
}

Medium/Arithmetic Slices.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
class Solution {
2-
public int numberOfArithmeticSlices(int[] A) {
3-
4-
int curr = 0, sum = 0;
5-
for (int i=2; i<A.length; i++)
6-
if (A[i]-A[i-1] == A[i-1]-A[i-2]) {
7-
curr += 1;
8-
sum += curr;
9-
}
10-
else {
11-
curr = 0;
12-
}
13-
14-
return sum;
2+
public int numberOfArithmeticSlices(int[] A) {
3+
int count = 0;
4+
for (int i = 0; i < A.length - 2; i++) {
5+
int diff = A[i + 1] - A[i];
6+
for (int j = i + 2; j < A.length; j++) {
7+
if (A[j] - A[j - 1] == diff) {
8+
count++;
9+
}
10+
else {
11+
break;
12+
}
13+
}
1514
}
15+
return count;
16+
}
1617
}

0 commit comments

Comments
 (0)