Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- Number Theory
- Class
- two pointers
- Math
- hash table
- SQL
- sorting
- simulation
- 코테
- Tree
- Stack
- java
- greedy
- database
- 파이썬
- string
- 코딩테스트
- Method
- bit manipulation
- Counting
- dynamic programming
- implement
- array
- Data Structure
- Matrix
- Binary Tree
- geometry
- 구현
- 자바
- Binary Search
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 2427. Number of Common Factors 본문
1. Input
1) int a
2) int b
2. Output
1) a와 b의 공약수의 개수를 반환
3. Constraint
1) 1 <= a, b <= 1000
4. Example
Input: a = 12, b = 6 -> Output: 4
설명: 12와 6의 공약수는 1, 2, 3, 6이다.
5. Code
1) 첫 코드(2023/05/03)
class Solution {
public int commonFactors(int a, int b) {
int answer = 0;
for(int i=1 ; i<=Math.min(a,b) ; i++)
if(a%i==0 && b%i==0)
answer++;
return answer;
}
}
2) 최대공약수를 먼저 계산한 코드(2023/05/03)
class Solution {
public int commonFactors(int a, int b) {
int answer = 0, gcd = 1;
for(int i=Math.min(a,b) ; i>1 && gcd==1 ; i--)
if(a%i==0 && b%i==0)
gcd = i;
for(int i=1 ; i<=gcd ; i++)
if(gcd%i==0)
answer++;
return answer;
}
}
- 이게 더 좋다.
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 2437. Number of Valid Clock Times (0) | 2023.05.03 |
---|---|
[LeetCode/Easy] 2432. The Employee That Worked on the Longest Task (0) | 2023.05.03 |
[LeetCode/Easy] 2418. Sort the People (0) | 2023.05.03 |
[LeetCode/Easy] 2413. Smallest Even Multiple (0) | 2023.05.03 |
[LeetCode/Easy] 2399. Check Distances Between Same Letters (0) | 2023.05.03 |