面试基础算法、及编程 第一弹

// #  -*- coding:utf-8 -*- 
// #  @Author: Mr.chen([email protected]) 
// #  @Date: 2018-07-31 17:54:26 

// 注:此类题目均可要求手写或者 Computer Coding
// 第一弹,先介绍一些,相对来说简单一些但是非常基础但是需要注意细节的题目


/*
1、判断是否是素数 ? 使用根号进行判断。
*/
#include <math.h>

bool isPrime(int num)
{
    for (int i = 2; i < sqrt(num); i ++)
    {
        if (num % i == 0)
            return false;
    }
    return true;
}


/* 
2、合并两个有序的数组
*/
int * MergSort(int *A,int * B, int n, int m)
{
    int *p1 = A, *p2 = B, i;
    int *p3 = new int[m+n]();
    for (i = 0; i< m, i< n; i++)
    {
        if(*p1 <= *p2)
        {
            *p3 ++ = *p1 ++;
        }
        else 
        {
            *p3 ++ = *p2 ++;
        }
    }
    if (i == n)
    {
        while(i < m)
        {
            *p3 ++ = *p2 ++;
        }
    }
    else (i == m)
    {
        while(i < n)
        {
            *p3 ++ = *p1 ++;
        }
    }
    return p3 - m -n;
}



/* 
3、冒泡排序
*/
void BubbleSort(Elemtype A[],int n)
{
    // 从小到大排列
    for(int i= 0; i < n-1; i++)
    {
        flag = false;
        for( int j = n-1; j > i; j--)
        {
            if(A[j-1].key > A[j].key)
            {
                swap(A[j-1],A[j]);
                flag = true;
            }
        }
        if (false == flag)      // 当默认有序时,停止排序
            return;
    }
}



/* 
4、快速排序  === > 分治法, 实现方式之一
*/
void QuickSort(ElemType A[],int low,int high)
{
    if (low < high)
    {
        int pivotpos = Partition(A, low, high);
        QuickSort(A,low,pivotpos -1);
        QuickSort(A,pivotpos+1,high)
    }
}

int Partition(Elemtype A[],int low, int high)
{
    ElemType pivot = A[low];      //  第一个元素作为杻轴
    while(low < high)
    {
        while(low < high && A[high] >= pivot) -- high;
        A[low] = A[high];
        while(low < high && A[low] <= pivot)  ++ low;
        A[high] = A[low];
    } 
    A[low] = pivot;
    return low;
}



/* 
5、判断单链表是否有环
*/

struct ListNode 
{
    int value;
    ListNode * pNext;
};

bool isloop(ListNode * head)
{
    ListNode * n1 = head;
    ListNode * n2 = head;
    while(NULL != n2->pNext)
    {
        n1 = n1 ->pNext;
        if(n1 == n2)        // 有环
            return true;
    }
    if (NULL == n2->pNext)
    {
        return false;
    }
}



/* 
6、自定义实现 strcpy() 函数
*/
char * strcpy(char * strDestination,const char * strSource)
{
    assert(NULL != strDestination && NULL != strSource);
    char *strD = strDestination;
    while(( *strDestination ++ = * strSource ++) != '\0')
    return strD;
}



/* 
7、二叉树的遍历 (其中前、中、后都是针对的是根节点而言的)
实现递归后序、和非递归后序
*/

struct BiTreeNode
{
    int data;
    BiTreeNode *lchild;
    BiTreeNode *rchild;
};

// 递归
void postOrder(BiTreeNode *T)
{
    if (NULL != T)
    {
        postOrder(T->lchild);
        postOrder(T->rchild);
        cout<< T->data << " ";
    }
}

#include <stack>
using std::stack;

// 非递归后序
void postOrder(BiTreeNode * T)
{
    stack<BiTreeNode *> s;
    BiTreeNode *p = T, *r = NULL,temp;
    while(p || s.empty())
    {
        if(p)
        {
            s.push(p);
            p = p->lchild;
        }
        else 
        {
            p = s.top();      // 向右,取栈顶节点
            if(p->rchild && p->rchile ! = r)
            {
                p = p->rchild;
                s.push(p);
                p = p->lchild;      // 再走到最左边
            }
            else  
            {
                p = s.top();        // 否则弹出节点并访问
                s.pop();
                cout<< p->data;
                r = p;              // 记录最近访问的结点
                p = NULL;
            }
        }
    }    
}



/* 
8、实现单例模式
*/
class Singleton 
{
public:
    static Singleton * getInstance()
    {  return instance;   }

private:
    Singleton() {}
    Singleton(const Singleton &)() {}
    Singleton & operator=(const Singleton & )() {}
private:
    static Singleton * instance;
};

// 初始化
Singleton * Singleton::instance = new Singleton();



/* 
9、补全下列的类中各个成员函数的实现
*/
class CMyString 
{
public:
    CMyString(const char *pdata = NULL);
    CMyString(const CMyString & other);
    ~CMyString();
    CMyString & CMyString::operator=(const CMyString & str);

private:
    char * m_pdata;
};

// 实现如下:
CMyString::CMyString(const char *pData)
{
    if(NULL == pData)
    {
        m_pdata = new char[1];
        *m_pdata = '\0';
    }else  
    {
        int length = strlen(pdata);
        m_pdata = new char[length + 1];
        strcpy(m_pdata,pdata);
    }
}

CMyString::CMyString(const CMyString & other)
{
    int iLen = strlen(other.m_pdata);
    m_pdata = new char[iLen + 1];
    strcpy(m_pdata,other.m_pdata);
}

CMyString::~CMyString()
{
    delete []m_pdata;
}

CMyString & CMyString::operator=(const CMyString & str)
{
    if(this == &str)
        return *this;
    delete []m_pdata;
    m_pdata = NULL;
    m_pdata = new char[strlen(str.m_pdata) + 1];
    strcpy(m_pdata,str.m_pdata);
    return *this;
}

猜你喜欢

转载自blog.csdn.net/smilejiasmile/article/details/81319586