Last active
July 30, 2024 14:35
-
-
Save WhiteHyun/12f4aa10b53bf8d37abb9055a6fb471f to your computer and use it in GitHub Desktop.
LeetCode - 2191. Sort the Jumbled Numbers.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| final class LeetCode2191 { | |
| typealias Item = (original: Int, mapped: Int) | |
| func sortJumbled(_ mapping: [Int], _ nums: [Int]) -> [Int] { | |
| nums | |
| .map { convert($0, using: mapping) } | |
| .sorted { | |
| $0.mapped < $1.mapped | |
| } | |
| .map(\.original) | |
| } | |
| private func convert(_ originalNumber: Int, using mapping: [Int]) -> Item { | |
| var mappedNumber = 0 | |
| var number = originalNumber | |
| var base = 1 | |
| repeat { | |
| mappedNumber += mapping[number % 10] * base | |
| number /= 10 | |
| base *= 10 | |
| } while number != 0 | |
| return (originalNumber, mappedNumber) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment