17.09第k个数 Posted on 2020-04-20 | In bfs Words count in article: 157 | Reading time ≈ 1 下面是题目 下面是题目给出的模板123456class Solution {public: int getKthMagicNumber(int k) { }}; 用BFS+set可以完美遍历,真的太爽了1234567891011121314151617181920class solution{public: int getKthMagicNumber(int k) {//有个小细节需要注意,那就是s.begin()返回的是迭代器 //如果要在迭代器中取物,那么就要用 * //可能数字会很大,我们用long long 存储 set<long long>s; s.insert(1); int ans = 0; while(k--) {//利用set,完美解决了去重复、小到大排序的问题 ans = *s.begin(); s.erase(s.begin); s.insert(ans*3); s.insert(ans*5); s.insert(ans*7); } return ans; } }; -------------本文结束,感谢您的阅读------------- Post author: Jason Post link: https://jasonxqh.github.io/2020/04/20/17-09%E7%AC%ACk%E4%B8%AA%E6%95%B0/ Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.