Skip to content

[Algorithm] 하샤드 수 #83

@hwangJi-dev

Description

@hwangJi-dev

💬 문제

[코딩테스트 연습 - 하샤드 수](https://school.programmers.co.kr/learn/courses/30/lessons/12947)


💬 Idea

  • 정수 x를 String으로 바꾼 후 배열로 변환하여 자릿수를 얻고, 해당 배열을 Int로 다시 형변환을 한다.
  • 그 후 Int형 배열을 차례대로 더해주어 도출된 결과값으로 x가 나누어진다면 true를, 그렇지 않다면 false를 리턴한다.

💬 풀이

func solution(_ x:Int) -> Bool {
    return x % Array(String(x)).map({ Int(String($0))! }).reduce(0, +) == 0 ? true : false
}

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions