Skip to content

Commit c07fc80

Browse files
committed
홀수 카운트
1 parent 880b84b commit c07fc80

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

CountOdds.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class CountOdds {
2+
public static void main(String[] args) {
3+
System.out.println(countOdds(3, 7));
4+
}
5+
6+
//my solution
7+
public static int countOdds(int low, int high) {
8+
if (low % 2 == 1 || high % 2 == 1)
9+
return (high - low) / 2 + 1;
10+
return (high - low) / 2;
11+
}
12+
}
13+
14+
//best solution
15+
class Solution {
16+
public int countOdds(int low, int high) {
17+
if (low == 0 && high == 0) return 0;
18+
int n = high - low + 1;
19+
if (n % 2 == 0) return n/2;
20+
if (high % 2 == 0) return n/2;
21+
return n/2 + 1;
22+
}
23+
}

0 commit comments

Comments
 (0)