原文: http://www.cnblogs.com/JuneWang/p/3773880.htmlhtml
//copyright@July 2013/9/24 void* memcpy(void *dst, const void *src, size_t count) { //安全檢查 assert( (dst != NULL) && (src != NULL) ); unsigned char *pdst = (unsigned char *)dst; const unsigned char *psrc = (const unsigned char *)src; //防止內存重複 assert(!(psrc<=pdst && pdst<psrc+count)); assert(!(pdst<=psrc && psrc<pdst+count)); while(count--) { *pdst = *psrc; pdst++; psrc++; } return dst; }
struct node { int elem; node* next; };
9月11日, 京東:node
談談你對面向對象編程的認識linux
struct Node{ Node* parent; Node* firstChild; // 孩子節點 Node* sibling; // 兄弟節點 }
typedef struct list { int data; //數據字段 list *middle; //指向鏈表中某任意位置元素(可指向本身)的指針 list *next;//指向鏈表下一元素 } list;
var tt = 'aa'; function test() { alert(tt); var tt = 'dd'; alert(tt); } test();
struct rect { // axis alignment assumed // bottom left is (x[0],y[0]), top right is (x[1],y[1]) double x [2]; double y [2]; }; template <typename T> T const& min (T const& x, T const& y) { return x<y ? x : y; } template <typename T> T const& max (T const& x, T const& y) { return x>y ? x : y; } // return type changed to handle non-integer rects double area (rect const& a, rect const& b) { // perfectly adjacent rects are considered having an intersection of 0 area double const dx = min(a.x[1],b.x[1]) - max(a.x[0],b.x[0]); double const dy = min(a.y[1],b.y[1]) - max(a.y[0],b.y[0]); return dx>=0&&dy>=0 ? dx*dy : -1; }
4**9 的筆試題,比較簡單:
1.求鏈表的倒數第二個節點
2.有一個整數數組,求數組中第二大的數ios
template <typename Comparable> Comparable maxprod( const vector<Comparable>&v) { int i; Comparable maxProduct = 1; Comparable minProduct = 1; Comparable maxCurrent = 1; Comparable minCurrent = 1; //Comparable t; for( i=0; i< v.size() ;i++) { maxCurrent *= v[i]; minCurrent *= v[i]; if(maxCurrent > maxProduct) maxProduct = maxCurrent; if(minCurrent > maxProduct) maxProduct = minCurrent; if(maxCurrent < minProduct) minProduct = maxCurrent; if(minCurrent < minProduct) minProduct = minCurrent; if(minCurrent > maxCurrent) swap(maxCurrent,minCurrent); if(maxCurrent<1) maxCurrent = 1; //if(minCurrent>1) // minCurrent =1; } return maxProduct; }
/* 給定一個整數數組,有正有負數,0,正數組成,數組下標從1算起 求最大連續子序列乘積,並輸出這個序列,若是最大子序列乘積爲負數,那麼就輸出-1 用Max[i]表示以a[i]結尾乘積最大的連續子序列 用Min[i]表示以a[i]結尾乘積最小的連續子序列 由於有複數,因此保存這個是必須的 */ void longest_multiple(int *a,int n){ int *Min=new int[n+1](); int *Max=new int[n+1](); int *p=new int[n+1](); //初始化 for(int i=0;i<=n;i++){ p[i]=-1; } Min[1]=a[1]; Max[1]=a[1]; int max_val=Max[1]; for(int i=2;i<=n;i++){ Max[i]=max(Max[i-1]*a[i],Min[i-1]*a[i],a[i]); Min[i]=min(Max[i-1]*a[i],Min[i-1]*a[i],a[i]); if(max_val<Max[i]) max_val=Max[i]; } if(max_val<0) printf("%d",-1); else printf("%d",max_val); //內存釋放 delete [] Max; delete [] Min; }
Q爲0
說明數組中至少有兩個0,那麼 N-1個數的乘積只能爲0,返回0;
Q爲正數
返回 Q,由於若是以0替換此時 AN -1中的任一個數,所獲得的 PN -1爲0,必然小於 Q;
Q爲負數
若是以0替換此時 AN -1中的任一個數,所獲得的 PN -1爲0,大於 Q,乘積最大值爲0。
2. P爲負數c++
根據「負負得正」的乘法性質,天然想到從 N個整數中去掉一個負數,使得 PN -1爲一個正數。而要使這個正數最大,這個被去掉的負數的絕對值必須是數組中最小的。咱們只須要掃描一遍數組,把絕對值最小的負數給去掉就能夠了。
3. P爲正數web
相似地,若是數組中存在正數值,那麼應該去掉最小的正數值,不然去掉絕對值最大的負數值。上面的解法採用了直接求N個整數的乘積P,進而判斷P的正負性的辦法,可是直接求乘積在編譯環境下每每會有溢出的危險(這也就是本題要求不使用除法的潛在 用意),事實上可作一個小的轉變,不須要直接求乘積,而是求出數組中正數(+)、負數(-)和0的個數,從而判斷P的正負性,其他部分與以上面的解法相 同。
union{ int i; unsigned char ch[2]; }Student; int main() { Student student; student.i=0x1420; printf("%d %d",student.ch[0],student.ch[1]); return 0; }
//假設str已經有序,from 一直很安靜 void perm(char *str, int size, int resPos) { if(resPos == size) print(result); else { for(int i = 0; i < size; ++i) { result[resPos] = str[i]; perm(str, size, resPos + 1); } } }
void fun() { unsigned int a = 2013; int b = -2; int c = 0; while (a + b > 0) { a = a + b; c++; } printf("%d", c); }
#include <STDIO.H> #include <WINDOWS.H> int Friends(int n, int m , int* r[]); int main(int argc,char** argv) { int r[5][2] = {{1,2},{4,3},{6,5},{7,8},{7,9}}; printf("有%d個朋友圈。\n",Friends(0,5,(int**)r)); return 0; } int Friends(int n, int m, int* r[]) // 注意這裏的參數很奇葩 { int *p = (int*)malloc(sizeof(int)*m*3); memset(p,0,sizeof(int)*m*3); int i = 0; int iCount = 0; int j = 0; int * q = (int*)r; // 這裏很巧妙 將二維指針 強轉爲一維指針 for (i=0;i<m;++i) { for (j=0;j<2;++j) { p[i*3+j]=q[i*2+j]; // 注意這裏二維數組向一維數組的轉換 } p[i*3+j] = 0; } bool bFlag = false; for (i=0;i<m;++i) { bFlag = false; if (p[i*3+2]==1) { bFlag = true; } p[i*3+2] = 1; for (j=0;j<m;++j) { if (i==j) { continue; } if (p[i*3]==p[j*3] || p[i*3] == p[j*3+1] || p[i*3+1] == p[j*3+0] || p[i*3+1] == p[j*3+1]) { if (p[j*3+2]==1) { bFlag = true; } p[j*3+2] = 1; } } if (!bFlag) { ++iCount; } } free(p); return iCount; }
2.1 // 採用兩兩比較的思路(目前沒想到更好的) if (a <= b) { if (b <= c) return b; else { if (a <=c) return c; else return a; } } else { if (a <= c) return a; else { if (b <= c) return c; else return b; } }
//void * memmove ( void * destination, const void * source, size_t num );) //是<string.h>的標準函數,其做用是把從source開始的num個字符拷貝到destination。 //最簡單的方法是直接複製,可是因爲它們可能存在內存的重疊區,所以可能覆蓋了原有數據。 //好比當source+count>=dest&&source<dest時,dest可能覆蓋了原有source的數據。 //解決辦法是從後往前拷貝。 //對於其它狀況,則從前日後拷貝。 void* memmove(void* dest, void* source, size_t count) { void* ret = dest; if (dest <= source || dest >= (source + count)) { //正向拷貝 //copy from lower addresses to higher addresses while (count --) *dest++ = *source++; } else { //反向拷貝 //copy from higher addresses to lower addresses dest += count - 1; source += count - 1; while (count--) *dest-- = *source--; } return ret; }
10月10日人人網面試題
第一面:
一、(1)++i 和 i++,那個效率高?
(2)++++i,i++++,哪一個是合法的?
(3)實現int型的++i 和 i++操做。
二、一段程序,求輸出。(考察靜態變量和模版類)面試
int g = 0; template<typename T> class B { public: int static fun() { static int value = ++g; return value; } }; int main() { cout << B<int>::fun() << endl; cout << B<char>::fun() << endl; cout << B<float>::fun() << endl; cout << B<int>::fun() << endl; cout << B<long>::fun() << endl; return 0; }
//copyright @wumuzi520 //從n個數中選取m個數的組合數 void Combination(int arr[], int nLen, int m, int out[], int outLen) { if(m == 0) { for (int j = 0; j < outLen; j++) { cout << out[j] << "\t"; } cout << endl; return; } for (int i = nLen; i >= m; --i) //從後往前依次選定一個 { out[m-1] = arr[i-1]; //選定一個後 Combination(arr,i-1,m-1,out,outLen); // 從前i-1個裏面選取m-1個進行遞歸 } } void PrintCombination(int arr[], int nLen, int m) { int* out = new int[m]; Combination(arr,nLen,m,out,m); delete [] out; }
static void Main(string[] args) { double k = 5; double n = 2, m = k; while (n != m) { m = k / n; n = (m + n) / 2; } }
西芹_new<huangxy10@qq.com> 0:55:40 // 10_15.cpp : 定義控制檯應用程序的入口點。 // #include "stdafx.h" #include <iostream> using namespace std; #define N 5 int map[5][5]={ {2,0,8,0,2}, {0,0,0,0,0}, {0,3,2,0,0}, {0,0,0,0,0}, {2,0,8,0,2}}; int sumMax=0; int p1x=0; int p1y=0; int p2x=0; int p2y=0; int curMax=0; void dfs( int index){ if( index == 2*N-2){ if( curMax>sumMax) sumMax = curMax; return; } if( !(p1x==0 && p1y==0) && !(p2x==N-1 && p2y==N-1)) { if( p1x>= p2x && p1y >= p2y ) return; } //right right if( p1x+1<N && p2x+1<N ){ p1x++;p2x++; int sum = map[p1x][p1y]+map[p2x][p2y]; curMax += sum; dfs(index+1); curMax -= sum; p1x--;p2x--; } //down down if( p1y+1<N && p2y+1<N ){ p1y++;p2y++; int sum = map[p1x][p1y]+map[p2x][p2y]; curMax += sum; dfs(index+1); curMax -= sum; p1y--;p2y--; } //rd if( p1x+1<N && p2y+1<N ) { p1x++;p2y++; int sum = map[p1x][p1y]+map[p2x][p2y]; curMax += sum; dfs(index+1); curMax -= sum; p1x--;p2y--; } //dr if( p1y+1<N && p2x+1<N ) { p1y++;p2x++; int sum = map[p1x][p1y]+map[p2x][p2y]; curMax += sum; dfs(index+1); curMax -= sum; p1y--;p2x--; } } int _tmain(int argc, _TCHAR* argv[]) { curMax = map[0][0]; dfs(0); cout <<sumMax-map[N-1][N-1]<<endl; return 0; }
10月15日,網新恆天筆試題
1.不要使用庫函數,寫出void *memcpy(void *dst, const void *src, size_t count),其中dst是目標地址,src是源地址。
點評:下面是nwpulei寫的代碼:算法
void* memcpy(void *dst, const void *src, size_t count) { assert(dst != NULL); assert(src != NULL); unsigned char *pdst = (unsigned char *)dst; const unsigned char *psrc = (const unsigned char *)src; assert(!(psrc<=pdst && pdst<psrc+count)); assert(!(pdst<=psrc && psrc<pdst+count)); while(count--) { *pdst = *psrc; pdst++; psrc++; } return dst; }
連接:http://blog.csdn.net/nwpulei/article/details/8090136。
2.給定一個字符串,統計一下哪一個字符出現次數最大。
3.咱們不知道Object類型的變量裏面會出現什麼內容,請寫個函數把Object類型轉換爲int類型。sql
Int CalculateStringDistance(string strA, int pABegin, int pAEnd, string strB, int pBBegin, int pBEnd) { if(pABegin > pAEnd) { if(pBBegin > pBEnd) return 0; else return pBEnd – pBBegin + 1; } if(pBBegin > pBEnd) { if(pABegin > pAEnd) return 0; else return pAEnd – pABegin + 1; } if(strA[pABegin] == strB[pBBegin]) { return CalculateStringDistance(strA, pABegin + 1, pAEnd, strB, pBBegin + 1, pBEnd); } else { int t1 = CalculateStringDistance(strA, pABegin, pAEnd, strB, pBBegin + 1, pBEnd); int t2 = CalculateStringDistance(strA, pABegin + 1, pAEnd, strB,pBBegin, pBEnd); int t3 = CalculateStringDistance(strA, pABegin + 1, pAEnd, strB,pBBegin + 1, pBEnd); return minValue(t1,t2,t3) + 1; } }
上面的遞歸程序,有什麼地方須要改進呢?在遞歸的過程當中,有些數據被重複計算了。好比,若是開始咱們調用CalculateStringDistance(strA,1, 2, strB, 1, 2),下圖是部分展開的遞歸調用。shell
能夠看到,圈中的兩個子問題被重複計算了。爲了不這種沒必要要的重複計算,能夠把子問題計算後的解存儲起來。如何修改遞歸程序呢?仍是DP!請看此連接:http://www.cnblogs.com/yujunyong/articles/2004724.html。
三、此外,關於這個「編輯距離」問題的應用:搜索引擎關鍵字查詢中拼寫錯誤的提示,能夠看下這篇文章:http://www.ruanyifeng.com/blog/2012/10/spelling_corrector.html。「關於什麼是「編輯距離」:一個快速、高效的Levenshtein算法實現,這個是計算兩個字符串的算法,Levenshtein距離又稱爲「編輯距離」,是指兩個字符串之間,由一個轉換成另外一個所需的最少編輯操做次數。固然,次數越小越類似。這裏有一個BT樹的數據結構,挺有意思的:http://blog.notdot.net/2007/4/Damn-Cool-Algorithms-Part-1-BK-Trees」
最後,Lucene中也有這個算法的實現(我想,通常的搜索引擎通常都應該會有此項拼寫錯誤檢查功能的實現):http://www.bjwilly.com/archives/395.html。
四、擴展:面試官還能夠繼續問下去:那麼,請問,如何設計一個比較兩篇文章類似性的算法?(這個問題的討論能夠看看這裏:http://t.cn/zl82CAH)
10月28日,微軟三面題「順祝,老媽明天生日快樂!」:
找一個點集中與給定點距離最近的點,同時,給定的二維點集都是固定的,查詢可能有不少次,時間複雜度O(n)沒法接受,請設計數據結構和相應的算法。
相似於@陳利人:附近地點搜索,就是搜索用戶附近有哪些地點。隨着GPS和帶有GPS功能的移動設備的普及,附近地點搜索也變得煊赫一時。在龐大的地理數 據庫中搜索地點,索引是很重要的。可是,咱們的需求是搜索附近地點,例如,座標(39.91, 116.37)附近500米內有什麼餐館,那麼讓你來設計,該怎麼作?
點評:R樹「從B樹、B+樹、B*樹談到R 樹」仍是KD樹「從K近鄰算法、距離度量談到KD樹、SIFT+BBF算法」?