leetcode-1329,1356,1333(学会在map里排序)(自定义排序算法,用auto const& a,这样会快很多)

1:1329:https://leetcode-cn.com/problems/sort-the-matrix-diagonally/

题目:

思路1:用map将对角线上的元素都存储起来,然后排序,再放回原来的位置,

代码:

收获:这个对map的排序,sort(hashmap[s.first].begin(),hashmap[s.first].end());学习上了

思路2:一次遍历就搞定,在遇到对角线元素时,就一次性直接把该条线遍历完,然后排序该对角线的元素,重新填入

代码:

收获:这种row++,col++,最后再还原回去,很值得学习。

1356:https://leetcode-cn.com/problems/sort-integers-by-the-number-of-1-bits/

题目:

思路:先统计各个数代表二进制1的个数,用map存储数量以及这个数本身,再对map进行排序,排序同1329的sort()

代码:

收获:二进制判断1的个数,排序的写法

1333:https://leetcode-cn.com/problems/filter-restaurants-by-vegan-friendly-price-and-distance/

题目:

思路:直接判断,将符合条件的放入结果集,最后对结果集进行排序。

代码:

收获:sort排序的自定义的写法,以及const auto &,return 的写法

原创文章 23 获赞 23 访问量 1716

猜你喜欢

转载自blog.csdn.net/LLM1602/article/details/105097685