剑指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;        //最小值栈
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值