劍指offer(二)

11.二進制中1的個數

在這裏插入圖片描述

class Solution {
public:
     int  NumberOf1(int n) {
         
         int ret = 0;
         
         for(int i=0; i<sizeof(int)*8; i++)        //不能使用 while(n != 0)  
                                                   //負數用補碼錶示,而移位是算術移位,如果是負數移位後會在最高位補1,那麼就不能正確統計個數,而且程序會死循環
         {
             if(n & 1 == 1)
                 ret++;
             n = n>>1;
         }
         
         return ret;
     }
};

12.數值的整數次方

在這裏插入圖片描述

遞歸
在這裏插入圖片描述

class Solution {
public:
    double Power(double base, int exponent) {
    
        bool isNegative = (exponent < 0)?true:false;
        exponent = abs(exponent);
        
        if(exponent == 0)
            return 1;
                
        double ret = Power(base, exponent/2);
        ret = (exponent%2 == 0)?ret*ret:ret*ret*base;
        
        if(isNegative)
            ret = 1/ret;
        return ret;
    }
};

13.調整數組順序使奇數位於偶數前面

在這裏插入圖片描述

開闢一個新數組,然後將調好序的數組賦值給原數組

class Solution {
public:
    void reOrderArray(vector<int> &array) {
        
        if(array.size() <= 1)
            return ;
        
        int num = 0;
        for(int i=0; i<array.size(); i++)
            if(array[i]%2 != 0)
                num++;
        
        vector<int> help(array.size(), -1);
        int index_even = num;
        int index_odd = 0;
        for(int i=0; i<array.size(); i++)
        {
            if(array[i]%2 != 0)
                help[index_odd++] = array[i];
            else
                help[index_even++] = array[i];
        }
            
        array.swap(help);
    }
};

14.鏈表中倒數第k個結點

在這裏插入圖片描述

/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
    
        if(!pListHead)
            return NULL;
        
        ListNode *pre = pListHead;
        ListNode *cur = pListHead;
        
        while(pre && k>0)
        {
            k--;
            pre = pre->next;
        }
            
        
        if(k != 0)
            return NULL;
        
        while(pre)
        {
            pre = pre->next;
            cur = cur->next;
        }
        
        return cur;
    }
};

15.反轉鏈表

在這裏插入圖片描述

/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {

        if(!pHead)
            return NULL;
        
        ListNode *dummyHead = new ListNode(-1);
        dummyHead->next = pHead;
        
        ListNode *cur = pHead->next;
        pHead->next = NULL;
        while(cur)
        {
            ListNode *tmp = cur->next;
            cur->next = dummyHead->next;
            dummyHead->next = cur;
            
            cur = tmp;
        }
        
        return dummyHead->next;
    }
};

16.合併兩個排序的鏈表

在這裏插入圖片描述

/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
    {
        if(!pHead1 && pHead2)
            return pHead2;
        if(pHead1 && !pHead2)
            return pHead1;
        
        ListNode *pHead3 = new ListNode(-1);
        ListNode *l1 = pHead1;
        ListNode *l2 = pHead2;
        ListNode *l3 = pHead3;
        
        while(l1 && l2)
        {
            if(l1->val <= l2->val)
            {
                ListNode *tmp = l1->next;
                l3->next = l1;
                l3 = l1;
                l1 = tmp;
            }
            else{
                ListNode *tmp = l2->next;
                l3->next = l2;
                l3 = l2;
                l2 = tmp;
            }
        }
        
        if(l1)
            l3->next = l1;
        else if(l2)
            l3->next = l2;
        
        
        return pHead3->next;
    }
};

17.樹的子結構

在這裏插入圖片描述

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
    {
        if(!pRoot1 || !pRoot2)
            return false;
        
        return isSame(pRoot1, pRoot2) || HasSubtree(pRoot1->left, pRoot2) || HasSubtree(pRoot1->right, pRoot2);
    }
    
    bool isSame(TreeNode *pRoot1, TreeNode *pRoot2)
    {
        if(!pRoot1 && pRoot2)
            return false;
        
        if(!pRoot2)
            return true;
        
        return pRoot1->val==pRoot2->val && isSame(pRoot1->left, pRoot2->left) && isSame(pRoot1->right, pRoot2->right);
    }
};

18.二叉樹的鏡像

在這裏插入圖片描述

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    void Mirror(TreeNode *pRoot) {

        if(!pRoot)
            return ;
        
        Mirror(pRoot->left);
        Mirror(pRoot->right);
        
        TreeNode *tmp = pRoot->left;
        pRoot->left = pRoot->right;
        pRoot->right = tmp;
    }
};

19. 順時針打印矩陣

在這裏插入圖片描述

在這裏插入圖片描述

class Solution {
public:
    vector<int> printMatrix(vector<vector<int> > matrix) {

        vector<int> ret;
        
        int row = matrix.size();
        int col = matrix[0].size();
        
        int left = 0;
        int top = 0;
        int right = col-1;
        int botton = row-1;
        
        while(left<=right && top<=botton)
        {
            for(int i=left; i<=right; i++)            //從左往右
                ret.push_back(matrix[top][i]);
            
            for(int i=top+1; i<=botton; i++)          //從上往下
                ret.push_back(matrix[i][right]);
            
            if(top != botton)                         //避免只有一行
            {
                for(int i=right-1; i>=left; i--)      //從右往左
                    ret.push_back(matrix[botton][i]);
            }
            
            if(left != right)                          //避免只有一列
            {
                for(int i=botton-1; i>top; i--)        //從下往上
                    ret.push_back(matrix[i][left]);
            }
            
            left ++;
            top ++;
            
            right --;
            botton --;
        }
        
        return ret;
    }
};

20. 包含min函數的棧

在這裏插入圖片描述

class Solution {
public:
    void push(int value) {
        dataS.push(value);
        
        if(minS.size() == 0)
            minS.push(value);
        else
            minS.push(minS.top()<value?minS.top():value);
    }
    void pop() {
        dataS.pop();
        minS.pop();
    }
    int top() {
        return dataS.top();
    }
    int min() {
        return minS.top();
    }
    
private:
    stack<int> dataS;        //數據棧
    stack<int> minS;        //最小值棧
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章