File tree 2 files changed +23
-27
lines changed
2 files changed +23
-27
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public String getHint (String secret , String guess ) {
3
3
int [] counter = new int [10 ];
4
- int cows = 0 ;
4
+ for (Character c : secret .toCharArray ()) {
5
+ counter [Character .getNumericValue (c )]++;
6
+ }
5
7
int bulls = 0 ;
8
+ int cows = 0 ;
6
9
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 )) {
10
11
bulls ++;
11
12
}
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 ++;
21
16
}
22
17
}
23
- return new StringBuilder (). append ( bulls ). append ( "A" ). append (cows ). append ( "B" ). toString () ;
18
+ return bulls + "A" + (cows - bulls ) + "B" ;
24
19
}
25
20
}
Original file line number Diff line number Diff line change 1
1
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
+ }
15
14
}
15
+ return count ;
16
+ }
16
17
}
You can’t perform that action at this time.
0 commit comments