注意pat类型的题目,return value直接是0 因为要提供的是main function 而不是功能类型的小function。这种题目需要的是printf n ,输出n,然后return 0。要特别小心一些,心情紧张的时候就容易犯这种小错误。
关于处理大规模的数据的输入输出。python程序在这方面很有优势。因为python的int类型支持任意长度的整数输入(只要memory足够)在输入的时候按照字符串来读取,之后转换成int类型就能够按照正常的运算来计算了。如果是c/c++的程序的话,就需要自己处理好多字符串相关的问题,以及基于字符串的四则运算,在比赛或者刷题的时候就浪费了很多时间,python几行代码就能搞定的事情,c++却需要写一大串代码。具体python的 super long integer algorithm 的实现过程还是比较复杂的,在做题的时候拿来急用确实是有很多优势,节省很多时间和精力。
#include<deque> deque<int> d d.push_back(value) d.push_front(value) d.pop_back(value) d.pop_front(value) d.back() //return last element d.fron() //return first element
vector init by the {} notation, this is convenience for writing the test case std::vector<std::vector<int>> grid{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}; we can also inter the empty vector by using {} direactly
auto iter = a.begin(); while (iter != a.end()) { if (*iter > 30) { iter = a.erase(iter); } else { ++iter; } } 还有另外一种操作是
for ( ; it != res.end(); ) { if (condition) { it = res.erase(it); } else { ++it; } }
一个需要特别注意的操作是remove the vector elements by value。这里有比较细致的介绍。简单的方法是find和erase结合,但是这样需要多次call erase,因为array是顺序排列的,每次call之后都需要将其中的元素移动,这样的开销比较大。比较推荐的方式是使用remove-erase pattern.
This is an example
std::vector<int> list = {3,4,5,6}; list.erase(std::remove(list.begin(), list.end(), 4), list.end());
It is worth noting that the remove operation do not actually delete the element or update the size of the container, it just move the elements in the containers and put the elements that does not satisfy the specified condition at the first several positions in the container.
This is an example to show the idea of the remove, we can see that it returns the new iterator that points to the end position of the new array with out containing the elements specified by the user.
int main() { std::vector<int> list = {3, 4, 5, 6, 4, 7, 8}; auto pend = std::remove(list.begin(), list.end(), 4); for (auto p = list.begin(); p != pend; ++p) { std::cout << ' ' << *p; } return 0; } // 3 5 6 7 8
关于set容器
http://blog.csdn.net/hnust_xiehonghao/article/details/7942541 http://blog.csdn.net/longshengguoji/article/details/8546286 输出的地方要特别注意一下 定义一个printSet 里面通过迭代器来进行操作 注意set使用到的题目做的还不是很多 set与multiset通过底层是红黑树 只要使用好了 往往能省好多事 比如 Contains Duplicate III 这个题 set 中的元素默认是ascending排列的 set 与 multiset 的关键区别是集合中是否可以包含重复的元素
关于map的一些常用操作(关键时候这个还是很好用的) 比如一个map<String,int> 插入或者检索某个元素的时候 直接map[str]=number就OK了 还是比较好用的 元素查找 map.find(key) 返回的是迭代器 如果没有 就返回 map.end() 注意这里迭代器形式的书写 // 定义一个迭代器 map<int,int>::iterator iter // 进行查找 iter = mapStudent.find(1); if(iter != mapStudent.end()) { // map 迭代器的第一个值为key 第二个值为value Cout<<”Find, the value is ”<<iter->second<<endl; } else { Cout<<”Do not Find”<<endl; } char数组转化成string 这个比较好用(这个很关键 因为map声明的时候 只是支持string类型的 而精细控制的时候用字符串数组又比较好 所以输入的时候可以用字符串数组 最后声明map的时候再转化为string类型) map.insert(make_pair(string(字符串数组),value)) 注意前面的make_pair的操作 或者直接 <mapneme>[key]=value 采用这种直接添加 key-value 值的形式定义 map的操作好好整理一下: http://www.oschina.net/question/234345_48876 map.find(key)操作会在map中查询key值如果查询失败会返回map.end() a simple way to adjust if a specifc value is in map is to use the m.count(key) to adjust if the return value is 0 如果查询成功会返回一个迭代器,这个迭代器又有两个元素iterator->first和iterator->second两个元素分别代表 关键字 以及 实际存储的数据(即value值) 清空map中的元素 map.clear() 关于map的遍历操作: http://blog.csdn.net/windren06/article/details/8141921 map<string,int>::iterator it; for(it=m.begin();it!=m.end();++it) cout<<"key: "<<it->first <<" value: "<<it->second<<endl; return 0;
another important fact that the default value of the coresponding type will be returned if the key not exist in the map, for the following code, the output vlaue is 0:
std::map<int , int > m; int b = m[1]; std::cout << a << std::endl;
pair and tuple
when solving the bfs question, instead of creating a customized new node, if there are only two or three elements in the tree node, we can use pair or tuple.
For pair, we can just create it by {v1,v2} pair<int, int> p={v1,v2} or use make_pair function or when we insert things into the vector with pair for each element, we can just use v.insert({v1,v2}), this is similar for tuple, the difference is that there are three values for tuple.
When access element, for pair, we can use p.first and p.second to get first and second element.
For tuple, it is a little bit different, we need to use get<0>(t) get<1>(t) and get<2>(t) to access each element in a tuple.
The stl have implemented multiple algorithm based on the iterator. Be careful about the defination for the begin and the end. If we use the vector as an exmaple, the begin() operation returns the first position of the current stl. the end() returns the past-the-end element in the vector container. That means there is no avalible coresponding elements there. The past-the-end element is the theoretical element that would follow the last element in the vector. It does not point to any element, and thus shall not be dereferenced. We usually use the end as a label position. This is a good resource to show the idea of the begin and end approach to get the iterator.
Take care some operation such as upper_bound which can be used to simplify the code, such as leetcode 981