思路
由题意可得,排序位置由两部分决定,起主要作用的是数字的权重,起次要作用的是数字本身值的大小,且把题目告诉了1 <= lo <= hi <= 1000,所以我么可以将每个数的权重*10000,再加上数字本本身,将所有数字经过这样的变换后存入一个新的数组,最后将这个数组排序
解题过程
我们需要返回的即是新数组中k-对应位置的那个数字对10000取模
Code
class Solution {public int getKth(int lo, int hi, int k) {int n=hi-lo+1;int arr[]=new int[n];for(int i=0;i<n;i++){int t=getnum(lo+i);arr[i]=t*10000+lo+i;}Arrays.sort(arr);return arr[k-1]%10000;}public int getnum(int num){int ans=0;while(num!=1){if(num%2==0){num=num/2;ans++;} else{num=num*3+1;ans++;}}return ans;}
}作者:菜卷
链接:https://leetcode.cn/problems/sort-integers-by-the-power-value/solutions/3027525/jiang-zheng-shu-an-quan-zhong-pai-xu-by-6dtpn/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。