huequad / swift-algorithm

1 stars 0 forks source link

179. Largest Number #26

Open zekexros opened 3 years ago

zekexros commented 3 years ago

https://leetcode.com/problems/largest-number/

ghis22130 commented 3 years ago

https://ghis22130.github.io/2021-08-02-LeetCode_179.-Largest-Number/

zekexros commented 3 years ago

https://github.com/zeke-iOS/AlgorithmPractice/blob/main/LeetCode/Leet-179.swift

lenaios commented 3 years ago

@ghis22130 @ghis22130 풀이 정말 깔끔하네요!👏

class Solution {
  func largestNumber(_ nums: [Int]) -> String {
    var numsArr = Array(nums).map(String.init)
    numsArr.sort { $0 + $1 > $1 + $0 }
    let answer = numsArr.reduce("") { $0 + $1 }
    if let number = Int(answer), number == 0 {
        return "0"
    }
    return answer
  }
}

reduce 대신 joined를 사용할 수 있다.