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

c++;每周一些题(3)

2016-05-14 17:51 489 查看



class Solution {
public:
ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
if(pListHead==NULL||k==0){
return NULL;
}
ListNode* fast=pListHead;
ListNode* slow=pListHead;
while(fast){
fast=fast->next;
if(k==0){
slow=slow->next;
}
else
--k;
}
if(k==0){
return slow;
}
return NULL;
}
};




class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
if(pHead==NULL)
return NULL;
ListNode* newHead=pHead;
ListNode* tmp=pHead->next;
newHead->next=NULL;
while(tmp){
ListNode* next=tmp->next;
tmp->next=newHead;
newHead=tmp;
tmp=next;
}
return newHead;
}
};




class Solution {
public:
ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
{
if(pHead1==NULL)
return pHead2;
if(pHead2==NULL)
return pHead1;
if(pHead1==NULL&&pHead2==NULL)
return NULL;
ListNode* tmp1=pHead1,*tmp2=pHead2,*tmp=NULL,*newHead=NULL;
while(tmp1&&tmp2){
if(tmp1->val>=tmp2->val){
if(newHead==NULL){
newHead=tmp2;
tmp=newHead;
}
else
{
tmp->next=tmp2;
tmp=tmp->next;
}
tmp2=tmp2->next;
}
else{
if(newHead==NULL){
newHead=tmp1;
tmp=newHead;
}else
{
tmp->next=tmp1;
tmp=tmp->next;
}
tmp1=tmp1->next;
}
}
if(tmp2)
tmp->next=tmp2;
if(tmp1)
tmp->next=tmp1;
return newHead;
}
};



题意分析:1.是子结构代表2个树 数据域相同 并不是指针相同
2.可能子树结束父树 未结束

3.逻辑必须清楚,活用&& ,||符号





/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
{
if(pRoot1==NULL||pRoot2==NULL)
return false;
if(pRoot1->val==pRoot2->val){
if(hasSubtree(pRoot1, pRoot2))
return true;
}
if(HasSubtree(pRoot1->left,pRoot2)||\
HasSubtree(pRoot1->right, pRoot2))
return true;
return false;
}
bool hasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
{
if((pRoot1==NULL&&pRoot2!=NULL))
return false;
if(pRoot2==NULL)
return true;
if(pRoot1->val==pRoot2->val)
{
if(hasSubtree(pRoot1->left,pRoot2->left)&&\
hasSubtree(pRoot1->right,pRoot2->right))
return true;
return false;
}
return false;
}
};



/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
void Mirror(TreeNode *pRoot) {
if(pRoot==NULL)
return;
TreeNode* tmp=pRoot->left;
pRoot->left=pRoot->right;
pRoot->right=tmp;
Mirror(pRoot->left);
Mirror(pRoot->right);

}
};



注意:用C++ vector 创建二维数组:
1.创建时用双重循环
2.操作时 写入数据必须用push_back();
3.读取数据 可用数组方法 arr[x][x];

class Solution {

public:

vector<int> printMatrix(vector<vector<int> > matrix) {
vector<int> arr;
if (matrix.size() == 0 || matrix[0].size() == 0)
return arr;
int num = matrix.size()*matrix[0].size() - 1;
int tag = 0, flag = 0;
int index = 0, row = 0, line = 0;
while (1){
if (flag == 0){
for (; row<((int)matrix[0].size() - tag); row++){
arr.push_back(  matrix[line][row]);
index++;
}
if (index > num)
return arr;
flag++;
line++;
row--;
}
if (flag == 1){
for (; line<((int)matrix.size() - tag); line++){
arr.push_back(matrix[line][row]);
index++;
}
if (index >num)
return arr;
flag++;
line--;
row--;
}
if (flag == 2){
for (; row >= tag; row--){
arr.push_back(matrix[line][row]);
index++;
}
if (index > num)
return arr;
flag++;
line--;
row++;
}

if (flag == 3){
for (; line>tag; line--){
arr.push_back(matrix[line][row]);
index++;
}
if (index > num)
return arr;
flag = 0;
line++;
row++;
}
tag++;
}

}
};



class Solution {
stack<int> _val,_min;
public:
void push(int value) {
_val.push(value);
if(_min.empty()){
_min.push(value);
}
else{
if(_min.top()>value)
_min.push(value);
}
}
void pop() {
if(!_val.empty()){
if(!_min.empty()&&_min.top()==_val.top())
_min.pop();
_val.pop();
}
}
int top() {
return 0;
}
int min() {
if(!_min.empty())
return _min.top();
else
return -1;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++;测试题