Skip to content

Instantly share code, notes, and snippets.

@WhiteHyun
Created July 30, 2024 15:00
Show Gist options
  • Select an option

  • Save WhiteHyun/cf1cec1fb94eaed5a0a40217e9b44f76 to your computer and use it in GitHub Desktop.

Select an option

Save WhiteHyun/cf1cec1fb94eaed5a0a40217e9b44f76 to your computer and use it in GitHub Desktop.
2191. Sort the Jumbled Numbers.swift using recursion
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