解题思路:
(1)使用map记录每个元素的步数
(2)利用vector对map进行按value排序
class Solution {
public:
int get_step(int a) {
int count=0;
while(a!=1) {
if(a%2==0) a=a/2;
else a=3*a+1;
count++;
}
return count;
}
int getKth(int lo, int hi, int k) {
unordered_map<int,int> mp;
for(int i=lo;i<=hi;i++) {
mp[i]=get_step(i);
}
typedef pair<int,int> pic;
vector<pic> v;
auto comp = [&](const pic &a,const pic &b) {
if(a.second<b.second) return true;
else if(a.second>b.second) return false;
else if(a.first<b.first) return true;
else return false;
};
for(auto it=mp.begin();it!=mp.end();it++) {
v.push_back(pic(it->first,it->second));
}
sort(v.begin(),v.end(),comp);
return v[k-1].first;
}
};