Skip to content

Instantly share code, notes, and snippets.

@WhiteHyun
Last active July 30, 2024 14:35
Show Gist options
  • Select an option

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

Select an option

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