leetcode经验

1.最大的k个元素/第k大的数字 用堆(priority_queue)或者快速排序

2.vector元素复制给map

unordered_map<int,int> m;
for(int num:nums)  //nums是vector
	++m[num];    //键是vector元素,值是元素出现的次数

3.a,b的中点用a+(b-a)/2,不用(a+b)/2,因为后者a+b可能溢出

4.对于string类型的要建map时,可以用vector来替代,索引为字母-‘a’,值字母为出现的次数

//string s, vector<int> counts
for (int i = 0; i < n; i++)  
    counts[s[i] - 'a']++;

5 动态规划中不是所给数据是几维的,递推公式就有几个变量(如leetcode300)。但是递归公式有几个变量,则代码应该就有几重循环,变量循环的内外层关系可能是不可以换的(如leetcode518) (未完全证实)

猜你喜欢

转载自blog.csdn.net/aikudexue/article/details/86616719