Skip to content

Commit 5a74e20

Browse files
committed
Added 1 solution & modified 2 solutions
1 parent 1656547 commit 5a74e20

File tree

4 files changed

+69
-53
lines changed

4 files changed

+69
-53
lines changed

Easy/Bulls and Cows.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public String getHint(String secret, String guess) {
3+
int[] counter = new int[10];
4+
int cows = 0;
5+
int bulls = 0;
6+
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+
bulls++;
11+
}
12+
else {
13+
if (counter[s] < 0) {
14+
cows++;
15+
}
16+
if (counter[g] > 0) {
17+
cows++;
18+
}
19+
counter[s]++;
20+
counter[g]--;
21+
}
22+
}
23+
return new StringBuilder().append(bulls).append("A").append(cows).append("B").toString();
24+
}
25+
}

Easy/Detect Capital.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
class Solution {
22
public boolean detectCapitalUse(String word) {
33
int n = word.length();
4-
boolean capitalFirst = Character.isUpperCase(word.charAt(0));
5-
int idx = 1;
6-
if (idx < n) {
7-
boolean capitalSecond = Character.isUpperCase(word.charAt(idx));
8-
for (idx = 1; idx < n; idx++) {
9-
if (capitalFirst && capitalSecond) {
10-
if (Character.isLowerCase(word.charAt(idx))) {
11-
return false;
12-
}
4+
if (n == 0) {
5+
return true;
6+
}
7+
boolean firstCaps = Character.isUpperCase(word.charAt(0));
8+
for (int i = 1; i < n; i++) {
9+
if (firstCaps) {
10+
if (Character.isUpperCase(word.charAt(1)) && !Character.isUpperCase(word.charAt(i))) {
11+
return false;
1312
}
14-
else if (capitalFirst && !capitalSecond) {
15-
if (Character.isUpperCase(word.charAt(idx))) {
16-
return false;
17-
}
13+
if (!Character.isUpperCase(word.charAt(1)) && Character.isUpperCase(word.charAt(i))) {
14+
return false;
1815
}
19-
else {
20-
if (Character.isUpperCase(word.charAt(idx))) {
21-
return false;
22-
}
16+
}
17+
else {
18+
if (Character.isUpperCase(word.charAt(i))) {
19+
return false;
2320
}
2421
}
2522
}

Medium/Bulls and Cows.java

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public double knightProbability(int N, int K, int r, int c) {
3+
double[][] dp = new double[N][N];
4+
int[] rowMoves = new int[]{2, 2, 1, 1, -1, -1, -2, -2};
5+
int[] colMoves = new int[]{1, -1, 2, -2, 2, -2, 1, -1};
6+
dp[r][c] = 1;
7+
while (K-- > 0) {
8+
double[][] copy = new double[N][N];
9+
for (int i = 0; i < N; i++) {
10+
for (int j = 0; j < N; j++) {
11+
for (int k = 0; k < 8; k++) {
12+
int destR = i + rowMoves[k];
13+
int destC = j + colMoves[k];
14+
if (destR >= 0 && destR < N && destC >= 0 && destC < N) {
15+
copy[destR][destC] += dp[i][j] / 8.0;
16+
}
17+
}
18+
}
19+
}
20+
dp = copy;
21+
}
22+
double ans = 0.0;
23+
for (double[] row : dp) {
24+
for (double val : row) {
25+
ans += val;
26+
}
27+
}
28+
return ans;
29+
}
30+
}

0 commit comments

Comments
 (0)