1. 程式人生 > >【劍指offer{25-30}】複雜連結串列的複製、字串的排列、陣列中出現次數超過一半的數字、最小的K個數、連續子陣列的最大和

【劍指offer{25-30}】複雜連結串列的複製、字串的排列、陣列中出現次數超過一半的數字、最小的K個數、連續子陣列的最大和

複雜連結串列的複製、字串的排列、陣列中出現次數超過一半的數字、最小的K個數、連續子陣列的最大和

複雜連結串列的複製

題目描述

  • 輸入一個複雜連結串列(每個節點中有節點值,以及兩個指標,一個指向下一個節點,另一個特殊指標指向任意一個節點),返回結果為複製後複雜連結串列的head。(注意,輸出結果中請不要返回引數中的節點引用,否則判題程式會直接返回空)

C++程式碼

/*
struct RandomListNode {
    int label;
    struct RandomListNode *next, *random;
    RandomListNode(int x) :
            label(x), next(NULL), random(NULL) {
    }
};
*/
class Solution {
public:
    RandomListNode* Clone(RandomListNode* pHead)
    {
        if(pHead==NULL)
        {
            return NULL;
        }
        RandomListNode *current = pHead;
        while(current!=NULL)
        {
            RandomListNode *clone = new RandomListNode(current->label);
            clone->next = current->next;
            current->next = clone;
            current = clone->next;
        }
        
        current = pHead;
        while(current!=NULL)
        {
            RandomListNode * clone = current->next;
            if(current->random!=NULL)
            {
                clone->random = current->random->next;
            }
            current = clone->next;
        }
        
        current = pHead;
        RandomListNode *CopyHead = pHead->next;
        while(current->next!=NULL)
        {
            RandomListNode *clone = current->next;
            current->next = clone->next;
            current = clone;
        }
        return CopyHead;
    }
};

字串的排列

題目描述

  • 輸入一個字串,按字典序打印出該字串中字元的所有排列。例如輸入字串abc,則打印出由字元a,b,c所能排列出來的所有字串abc,acb,bac,bca,cab和cba。

C++程式碼

class Solution {
public:
    vector<string> Permutation(string str) 
    {
        vector<string>res;
        if(str.size()==0)
        {
            return res;
        }
        sort(str.begin(),str.end());
        do
        {
            res.push_back(str);
        }while(next_permutation(str.begin(),str.end()));
        return res;
    }
};

陣列中出現次數超過一半的數字

題目描述

  • 陣列中有一個數字出現的次數超過陣列長度的一半,請找出這個數字。例如輸入一個長度為9的陣列{1,2,3,2,2,2,5,4,2}。由於數字2在陣列中出現了5次,超過陣列長度的一半,因此輸出2。如果不存在則輸出0。

C++程式碼

class Solution {
public:
    int MoreThanHalfNum_Solution(vector<int> numbers) 
    {
        sort(numbers.begin(),numbers.end());
        int left = 0;
        int right = numbers.size();
        int middle = (left +right)/2;
        int N = 0;
        N = count(numbers.begin(),numbers.end(),numbers[middle]);
        if(N>numbers.size()/2)
        {
            return numbers[middle];
        }
        else
        {
            return 0;
        }
    }
};

最小的K個數

題目描述

  • 輸入n個整數,找出其中最小的K個數。例如輸入4,5,1,6,2,7,3,8這8個數字,則最小的4個數字是1,2,3,4,。

C++程式碼

class Solution {
public:
    vector<int> GetLeastNumbers_Solution(vector<int> input, int k) 
    {
        vector<int>res;
        int len = input.size();
        if(k>len)
        {
            return res;
        }
        sort(input.begin(),input.end());
        res.insert(res.begin(),input.begin()+0,input.begin()+k);
        return res;
    }
};

連續子陣列的最大和

題目描述

  • HZ偶爾會拿些專業問題來忽悠那些非計算機專業的同學。今天測試組開完會後,他又發話了:在古老的一維模式識別中,常常需要計算連續子向量的最大和,當向量全為正數的時候,問題很好解決。但是,如果向量中包含負數,是否應該包含某個負數,並期望旁邊的正數會彌補它呢?例如:{6,-3,-2,7,-15,1,2,2},連續子向量的最大和為8(從第0個開始,到第3個為止)。給一個數組,返回它的最大連續子序列的和,你會不會被他忽悠住?(子向量的長度至少是1)

C++程式碼

class Solution {
public:
    int FindGreatestSumOfSubArray(vector<int> array) 
    {
        int maxValue = INT_MIN;
        int currentSum = 0;
        int len = array.size();
        for(int i = 0;i<len;i++)
        {
            currentSum = currentSum + array[i];
            if(currentSum>maxValue)
            {
                maxValue = currentSum;
            }
            if(currentSum<0)
            {
                currentSum = 0;
            }
        }
        return maxValue;
    }
};