vector的用法小结
2016-05-20 20:38
447 查看
#include<iostream> #include <vector> using namespace std; int main() { vector<int> ivec{1,2,3,4,5,6,7,8,9,10}; //令i依次为ivec中的每一个元素,并显示 for(auto i : ivec) cout << i << " "; cout << endl; //添加元素到ivec尾端 for(int i=11;i<=20;i++) ivec.push_back(i); //令i依次等于ivec中的每一个元素 for(auto &i : ivec) i *= i; //令i依次等于ivec中的每一个元素 for(auto i : ivec) cout << i << " "; cout << endl; //依然可以用下标去访问每一个元素 //也可写为for(decltype(ivec.size()) i=0;i!=20;i++) for(int i = 0;i<20;i++) cout << ivec[i] << " "; cout << endl; //.size()方法,显示拥有元素的个数 //.empty()方法,判空 cout << "ivec中有元素" << ivec.size() << "个。" << endl; //关于迭代器,用到再说。。。。 }
从尾到头打印链表
参与人数:9720时间限制:1秒空间限制:32768K
本题知识点: 链表
算法知识视频讲解
题目描述
输入一个链表,从尾到头打印链表每个节点的值。输入描述:
输入为链表的表头
输出描述:
输出为需要打印的“新链表”的表头
我的代码
/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : * val(x), next(NULL) { * } * }; */ class Solution { public: vector<int> printListFromTailToHead(struct ListNode* head) { vector<int> result; ListNode* p; p = head; while(p) { result.push_back(p->val); p=p->next; } vector<int> result2; for(auto i = result.end()-1;i!=result.begin()-1;i--) result2.push_back(*i); return result2; } };
抄的别人的
/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : * val(x), next(NULL) { * } * }; */ class Solution { public: vector<int> printListFromTailToHead(struct ListNode* head) { vector<int> result; ListNode* p; p = head; while(p) { result.insert(result.begin(),p->val); <span style="white-space:pre"> </span>p=p->next; } return result; } };
先用栈(这里用的deque 双端队列,比栈更强大),再转到vector 实现。
/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : * val(x), next(NULL) { * } * }; */ class Solution { public: vector<int> printListFromTailToHead(struct ListNode* head) { deque<int> result; ListNode* p; p = head; while(p) { result.push_front(p->val); p=p->next; } vector<int> result2; result2.assign(result.begin(),result.end()); return result2; } };
用两个栈实现队列
参与人数:7278时间限制:1秒空间限制:32768K
本题知识点: 栈 队列
算法知识视频讲解
题目描述
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。class Solution { public: void push(int node) { stack1.push(node); } int pop() { int a; if(stack2.empty()) { while(!stack1.empty()) { a = stack1.top(); stack1.pop(); stack2.push(a); } a = stack2.top(); //起初,到这里就结束了,始终通不过,,连return都忘了啊啊啊啊!!!!逻辑很重要。 stack2.pop(); return a; } else{ a = stack2.top(); stack2.pop(); return a; } } private: stack<int> stack1; stack<int> stack2; };
直接用的别人的思路
<分析>:
入队:将元素进栈A
出队:判断栈B是否为空,如果为空,则将栈A中所有元素pop,并push进栈B,栈B出栈;
如果不为空,栈B直接出栈。
结果 有一个点困了好久 见代码。
相关文章推荐
- 崔希凡javaWeb笔记day01~day03(2016年5月20日20:33:54)
- okhttp方法上传图片和接收图片
- 简单的 URL 编程
- Unity调用C++的Dll文件,Xcode的.a文件
- LayoutInflater的错误用法(Avoid passing null as the view root )
- bzoj4569[Scoi2016]萌萌哒
- 【学习心得】分块
- 整理篇
- error: no matching function for call to 'std::basic_ifstream<char>::open(std::string&)
- 逆---LanSet.exe
- Spring的组件装配
- HDU 2586 How far away ? (LCA)
- TensorFlow实战之:Quick Start
- Linux 命令行模式快捷键使用
- 静态碎片切换 和动态viewpage切换和view颜色切换
- microstation level2 0601_2
- Java的线程常用方法
- 安装tensorflow
- 加特征加正则
- POJ 2356 Find a multiple(鸽巢原理(抽屉原理))