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 | 29 |
30 | 31 |
Tags
- hash table
- SQL
- Math
- geometry
- implement
- string
- Counting
- Tree
- Class
- Data Structure
- 자바
- Stack
- bit manipulation
- 구현
- 파이썬
- greedy
- dynamic programming
- java
- sorting
- Number Theory
- 코딩테스트
- Method
- simulation
- Binary Tree
- Matrix
- two pointers
- Binary Search
- array
- database
- 코테
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 202. Happy Number 본문
1. Input
1) int n
2. Output
1) n이 happy number라면 true, 아니면 false 반환
// happy number란
- 각 자릿수의 제곱의 합을 구한다.
- 합이 1이 될 때까지 반복한다. 1이 아니라면 다시 각 자릿수의 제곱의 합을 구한다.
- 1이 되면 happy number가 된다.
3. Constraint
1) 1 <= n <= 2^31 - 1
4. Example
Input: n = 19 -> Output: true
설명:
1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1
5. Code
1) 첫 코드(2023/01/23)
import java.util.*;
boolean answer = false, duplicate = false;
long sum = 0, num = n;
ArrayList<Long> list = new ArrayList<Long>();
while(!answer && !duplicate){
while(num>0){
sum += (int)Math.pow(num%10, 2);
num /= 10;
}
num = sum; sum = 0;
if(num==1) answer = true;
else if(!list.contains(num)) list.add(num);
else duplicate = true;
}
return answer;
- 성능이 별로긴 하지만, 딱히 방법이 생각나지 않는다.
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 205. Isomorphic Strings (0) | 2023.01.23 |
---|---|
[LeetCode/Easy] 203. Remove Linked List Elements (0) | 2023.01.23 |
[LeetCode/Medium] 151. Reverse Words in a String (0) | 2023.01.20 |
[LeetCode/Medium] 137. Single Number II (0) | 2023.01.19 |
[프로그래머스/Lv.2] 스킬트리 (0) | 2023.01.17 |