您的位置:首页 > 编程语言 > C语言/C++

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++