面试基础算法、及编程 第一弹
生活随笔
收集整理的這篇文章主要介紹了
面试基础算法、及编程 第一弹
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
// # -*- coding:utf-8 -*-
// # @Author: Mr.chen(ai-chen2050@qq.com)
// # @Date: 2018-07-31 17:54:26 // 注:此類題目均可要求手寫或者 Computer Coding
// 第一彈,先介紹一些,相對來說簡單一些但是非常基礎但是需要注意細節的題目/*
1、判斷是否是素數 ? 使用根號進行判斷。
*/
#include <math.h>bool isPrime(int num)
{if (number <= 1) {return false;}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、冒泡排序,需要進行 n 輪調整,每輪從尾部開始,即 j 的索引處,每輪會有一個哨兵,flag,當一輪 flag 沒變時,說明已經有序了,即完成了排序。
*/
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;n2 = n2 ->pNext->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();// 法二 :利用全局靜態變量來實現單例模式,并且是線程安全的寫法,不過建議如果類較小,可以采用下列寫法,如下:
class Singleton
{
public:static Singleton * getInstance(){ static Singleton instance; // 全局靜態變量return &instance;}private:Singleton() {}Singleton(const Singleton &) {}Singleton & operator=(const Singleton & ) {}
private:static Singleton * instance;
};/*
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;
}/*
10. 用兩個棧模擬隊列的行為, 對外提供 Enqueue(back)和Dequeue(front)接口.讓棧A提供入隊功能,棧B提供出隊功能。入隊列:棧A。出隊列:如果棧B不為空,直接彈出棧B的棧頂數據;如果棧B為空,則依次彈出棧A的數據,放入棧B中,再彈出棧B的棧頂數據。具體編程實現如下所示:
*/template<class T>
struct MyQueue
{void Enqueue(T t){s1.push(t);}T Dequeue(){if(s2.empty()){if(0 == s1.size()){exit(1); // ensure visit s2.top() security.}while(!s1.empty()){s2.push(s1.top());s1.pop();}}T retVal = s2.top();s2.pop();return retVal;}std::stack<T> s1;std::stack<T> s2;
};/*
11. 三數之和:給你一個包含 n 個整數的數組?nums,判斷?nums?中是否存在三個元素 a,b,c ,使得?a + b + c = 0 ?請你找出所有和為 0 且不重復的三元組。注意:答案中不可以包含重復的三元 組。 思路:排序 + 雙指針 遍歷查找。
*/using std::vector;vector<vector<int>> threeSum(vector<int>& nums)
{int vecLen = nums.size();vector<vector<int>> vecIns;if(vecLen < 3)return vecIns;sort(nums.begin(), nums.end());// 枚舉 afor(int first=0; first<vecLen; ++first){// 需要和上一次枚舉的數不相同if( first > 0 && nums[first] == nums[first-1])continue;// c 對應的指針初始指向數組的最右端int thrid = vecLen - 1;int target = -nums[first];// 枚舉 bfor(int second = first +1; second < vecLen; ++ second){// 需要和上一次枚舉的數不相同if(second > first + 1 && nums[second] == nums[second-1])continue;// 需要保證 b 的指針在 c 的指針的左側while(second < thrid && nums[second] + nums[thrid] > target) --thrid;// 如果指針重合,隨著 b 后續的增加// 就不會有滿足 a+b+c=0 并且 b<c 的 c 了,可以退出循環if(second == thrid)break;if(nums[second] + nums[thrid] == target)vecIns.push_back({nums[first], nums[second], nums[thrid]});}}return vecIns;
}
總結
以上是生活随笔為你收集整理的面试基础算法、及编程 第一弹的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用计算路由的方法优化BI后台性能
- 下一篇: 2019年春运贵州道路客运预计达6700