C++;每周一些题(1)
2016-05-03 14:30
453 查看
#include<iostream> #include<string.h> using namespace std; class CMyString { public: char* _str; CMyString() :_str(NULL) {} CMyString(char * str) :_str(new[strlen(str)+1] ) { strcpy(_str,str); } CMyString(CMySting& tmp) :_str(new[strlen(tmp._str)+1]) { strcpy(_str,tmp._str); } CMyString operator=(CMyString tmp) { swap(_str,tmp._str); return *this; } ~CMyString() { if(_str!=NULL) delete _str; _str=NULL; } };
(单例模式)
#include<iostream> #include<string.h> #include<Mutex> using namespace std; singal::static singal* _this=NULL; class singal { char* _str; static singal* _this; public: static singal* GetSingal() { if(_this==NULL) { lock(); if(_this==NULL) _this=getsingal(); } return _this; } private: singal* getsingal() { singal* tmp=new singal; tmp->_str="i'm only one!"; return tmp; } singal(){} };
class Solution { public: bool Find(vector<vector<int> > array,int target) { int i = 0, j =array.size()-1; while (i <array[0].size()&&j>=0) { if (array[i][j]>target) { --j; } else if (array[i][j] == target) { return true; } else { ++i; j = array.size()-1; } } return false; } };
class Solution { public: struct TreeNode* reConstructBinaryTree(vector<int> pre, vector<int> in) { int i = 0; return ReConstructBinTree( pre, in, i, 0,in.size()-1); } private: struct TreeNode* ReConstructBinTree(vector<int> pre, vector<int> in,int& node,int start,int end) { if ( start>end||node==pre.size() ) return NULL; TreeNode* root = new TreeNode(pre[node++]); int i = 0; while (1) { if (in[i] == pre[node-1]) break; i++; } root->left = ReConstructBinTree(pre, in, node ,start, i-1); root->right = ReConstructBinTree(pre, in, node,i+1 ,end); return root; } };
class Solution { public: void replaceSpace(char *str,int length) { if (str == NULL || length <= 0) return; int i = 0; char* tmp = str; int strle = 0; while (*tmp) { if (*(tmp++) == ' ') i++; strle++; } int newlength = strle + i * 2; if (newlength>length) return; while (strle >= 0 && i>0) { if (str[strle] == ' ') { --i; str[newlength--] = '0'; str[newlength--] = '2'; str[newlength--] = '%'; } else { str[newlength--] = str[strle]; } --strle; } } };
/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : * val(x), next(NULL) { * } * }; */ class Solution { public: vector<int> list; vector<int> printListFromTailToHead(struct ListNode* head) { if(head!=NULL) { printListFromTailToHead(head->next); list.push_back(head->val); } return list; } };
class Solution { public: void push(int node) { stack1.push(node); } int pop() { int tmp=0; if(stack2.empty()){ while(!stack1.empty()){ stack2.push (stack1.top()); stack1.pop(); } } tmp=stack2.top(); stack2.pop(); return tmp; } private: stack<int> stack1; stack<int> stack2; };
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- 解析C++中派生的概念以及派生类成员的访问属性