Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions week1/1. 하샤드 수/Solution.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import java.util.Arrays;

/*
* 1. 하샤드 수
* https://programmers.co.kr/learn/courses/30/lessons/12947
*/
class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
int x = 19;
System.out.println(solution.solution(x));
}

public boolean solution(int x) {
return false;
public boolean solution(final int x) {
int sum = Arrays.asList(String.valueOf(x).split("")).stream().mapToInt(Integer::parseInt).sum();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

자리 수의 합을 구하기 위해 문자열로 바꾸고 그걸 List로 변형한뒤 stream api 를 이용하여 다시 숫자로 바꾸는 행위는 너무 비싼 비용을 지불하는 것 같습니다.

return x % sum == 0 ? true : false;
}

}


17 changes: 17 additions & 0 deletions week1/1. 하샤드 수/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 1. 하샤드 수
* https://programmers.co.kr/learn/courses/30/lessons/12947
*/
class Solution {
static solution = function (x) {
const sum = String(x)
.split("")
.reduce((a, c) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

숫자를 문자열로 변형한뒤 배열로 바꾸고 다시 recduce를 이용해 합을 구하는건 너무 비싼 비용을 지불 하는것 같습니다.

return Number(a) + Number(c);
});

return x % sum == 0 ? true : false;
};
}

console.log(Solution.solution(24))