2191. Sort the Jumbled Numbers
就给你一组mapping,其中mapping[i]=j
然后给你一个矩阵nums
请将nums中的值按照mapped value进行递增排序
并回传
如果两个mapping后的值一样,就按照原本相对order排序
思路:
就照做
建一个struct,里面存放原本的key、origin value、mapped value
然后再去排序
这题好像没什么好讲的,
我就只会这种烂方法
对阿
我要去干死阿喵喵了
golang code :
type point struct {
origin int
mapping int
key int
}
func sortJumbled(mapping []int, nums []int) []int {
points := make([]point, len(nums))
for key, val := range nums {
points[key].origin = val
points[key].key = key
tmp := 0
if val == 0 {
points[key].mapping = mapping[0]
} else {
n := (math.Floor(math.Log10(float64(val))))
div := int(math.Pow(10, n))
for i := 0; i <= int(n); i++ {
tmp = tmp*10 + mapping[(val/div)%10]
div /= 10
}
points[key].mapping = tmp
}
}
slices.SortFunc(points, func(i, j point) int {
if i.mapping == j.mapping {
return i.key - j.key
}
return i.mapping - j.mapping
})
for key := range nums {
nums[key] = points[key].origin
}
return nums
}