1. 程式人生 > >整理一些面試可能會遇到的演算法題目

整理一些面試可能會遇到的演算法題目

將兩個有序的單鏈表合併為一個有序的單鏈表,預設是按升序排列的。【兩路歸併排序(升序排列)  (平均/最差)時間複雜度O(NlogN)

typedef struct _Node_t
{
    struct _Node_t *next;
    int data;
}Node;

Node *Merge(Node *head1, Node *head2)//時間複雜度:O(nlogn)
{
    Node *head = NULL;
    
    if (NULL == head1)
    {
        return head2;
    }

    if (NULL == head2)
    {
        return head1;
    }
   
    while ((NULL != head1) && (NULL != head2))
    {
        if (head1->data < head2->data)
        {
            head = head1;
            head->next = Merge(head1->next, head2);
        }
        else
        {
            head = head2;
            head->next = Merge(head1, head2->next); 
        }
    }

    return head;
}

判斷單鏈表中是否有環,有的話找到環的入口點

定義兩個指標slow, fast。slow指標一次走1個結點,fast指標一次走2個結點。如果連結串列中有環,那麼慢指標一定會再某一個時刻追上快指標(slow == fast)。如果沒有環,則快指標會第一個走到NULL。

class Node {
    public Node next;
    public Object data;

    public static int sequence = 0;
}
/**
     * 快慢指標
     * @param head
     * @return
     */
    public static boolean checkCircle(Node head) {
        Node fast = null;
        Node slow = null;

        fast = head;
        slow = head;
        while (true) {
            // 慢指標移動一步
            if (null != slow.next) {
                slow = slow.next;
            } else {
                return false;
            }

            // 快指標移動兩步
            if (null != fast.next && null != fast.next.next) {
                fast = fast.next.next;
            } else {
                return false;
            }

            // 檢查是否相遇
            if (slow == fast) {
                return true;
            }
        }
    }

定義兩個指標p, q。p每走一個結點(即一步),q則從頭一直向後走,直到q走到NULL或p, q走到同一個結點但走過的步數不相同為止。此時q的步數就是環入口在結點中的位置。如果走到NULL則說明連結串列不存在環。

/**
     * 查詢環的起點
     * @param head
     * @return 返回元素的索引,從0開始。沒有找到返回-1
     */
    public static int findCircleEntry(Node head) {
        Node p = head; // 總是從頭開始
        Node q = head;

        int pSteps = 0;
        int qSteps = 0;
        while (null != q.next) {
            q = q.next;
            ++qSteps;

            // p從頭開始走
            while (null != p.next) {
                p = p.next;
                ++pSteps;

                // 當p與q指向同一個結點時
                if (p == q) {
                    // 如果走的步數不同,則這就是入口
                    if (pSteps != qSteps) {
                        return pSteps - 1;
                    } else {
                        // 走的步數相同,不是入口
                        break;
                    }
                }
            }

            p = head; // 回到頭結點
            pSteps = 0;
        }

        // 其中有一個指標走到了頭,說明沒有環
        return -1;
    }

斐波拉契數列遞迴實現的方法如下:

int Funct( int n )
{
	if(n==0) 
		return 1;
	if(n==1) 
		return 1;
	retrurn Funct(n-1) + Funct(n-2);
}

判斷一個字串是不是迴文

int IsReverseStr(char *aStr)
{
	
	if(aStr==NULL)
		return -1;

	int i,j;
    j=strlen(aStr);

    int found=1;
    for(i=0;i<j/2;i++)
    {
	     if(*(aStr+i)!=*(aStr+j-i-1)) 
	     {
	       found=0; 
	       break;
	     }
    }
    return found; 
}
6. 關鍵字 static 的作用是什麼?
這個簡單的問題很少有人能回答完全。在 C 語言中,關鍵字 static 有三個明顯 的作用:
1). 在函式體,一個被宣告為靜態的變數在這一函式被呼叫過程中維持其值不 變。
2). 在模組內(但在函式體外),一個被宣告為靜態的變數可以被模組內所用函 數訪問,但不能被模組外其它函式訪問。它是一個本地的全域性變數。

3). 在模組內,一個被宣告為靜態的函式只可被這一模組內的其它函式呼叫。那 就是,這個函式被限制在宣告它的模組的本地範圍內使用。 

非 C++內建型別 A 和 B,在哪幾種情況下 B 能隱式轉化為 A?

a. class B : public A { ......} // B 公有繼承自 A,可以是間接繼承的
b. class B { operator A( ); } // B 實現了隱式轉化為 A 的轉化
c. class A { A( const B& ); } // A 實現了 non-explicit 的引數為 B(可以有其 他帶預設值的引數)建構函式

d. A& operator= ( const A& ); // 賦值操作,雖不是正宗的隱式型別轉換, 但也可以勉強算一個

快速排序:

1.先從數列中取出一個數作為基準數

2.分割槽過程,將比這個數大的數全放到它的右邊,小於或等於它的數全放到它的左邊。

3.再對左右區間重複第二步,直到各區間只有一個數

void quickSort(int s[], int l, int r)  
{  
    if (l< r)  
    {        
        int i = l, j = r, x = s[l];  
        while (i < j)  
        {  
            while(i < j && s[j]>= x) // 從右向左找第一個小於x的數  
                j--;   
            if(i < j)  
                s[i++] = s[j];  
            while(i < j && s[i]< x) // 從左向右找第一個大於等於x的數  
                i++;   
            if(i < j)  
                s[j--] = s[i];  
        }  
        s[i] = x;  
        quickSort(s, l, i - 1); // 遞迴呼叫  
        quickSort(s, i + 1, r);  
    }  
}