Created
July 30, 2024 15:00
-
-
Save WhiteHyun/cf1cec1fb94eaed5a0a40217e9b44f76 to your computer and use it in GitHub Desktop.
2191. Sort the Jumbled Numbers.swift using recursion
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 { | |
| func sortJumbled(_ mapping: [Int], _ nums: [Int]) -> [Int] { | |
| zip(nums, nums.map { mapped(mapping, $0) }) | |
| .sorted { $0.1 < $1.1 } | |
| .map(\.0) | |
| } | |
| private func mapped(_ mapping: [Int], _ number: Int, _ base: Int = 1) -> Int { | |
| if number < 10 { | |
| base * mapping[number % 10] | |
| } else { | |
| base * mapping[number % 10] + mapped(mapping, number / 10, base * 10) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment