剑指offer刷题第三天

题目描述

大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。

n<=39

class Solution {
public:
    int Fibonacci(int n) {
        if(n <= 0){
            return 0;
        }
        int f1 = 1;
        int f2 = 1;
        int result = f2;
        for(int i = 2; i < n; i++){
            result = f1+f2;
            f1 = f2;
            f2 = result;
        }
        return result;
    }
};

题目描述

一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。

class Solution {
public:
    int jumpFloor(int number) {
        if(number <= 0){
            return 0;
        }
        if(number == 1){
            return 1;
        }
        int f1 = 1;
        int f2 = 2;
        int result = f2;
        for(int i = 2; i < number; i++){
            result = f1 + f2;
            f1 = f2;
            f2 = result;
        }
        return result;
    }
};

题目描述

一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。

class Solution {
public:
    int jumpFloorII(int number) {
        if(number <= 0){
            return 0;
        }
        int result = 1;
        for(int i = 1; i < number; i++){
            result *= 2;
        }
        return result;
    }
};

题目描述

我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?

class Solution {
public:
    int rectCover(int number) {
        if(number <= 0){
            return 0;
        }
        if(number == 1){
            return 1;
        }
        int f1 = 1;
        int f2 = 2;
        int result = f2;
        for(int i = 2; i < number; i++){
            result = f1 + f2;
            f1 = f2;
            f2 = result;
        }
        return result;
    }
};

题目描述

输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。

class Solution {
public:
     int  NumberOf1(int n) {
         int result = 0;
         while(n){
             result++;
             n = n & (n - 1);
         }
         return result;
     }
};

题目描述

给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。

class Solution {
public:
    double Power(double base, int exponent) {
        if(base == 0){
            return 0;
        }
        if(exponent == 0){
            return 1;
        }
        if(exponent < 0){
            base = 1/base;
            exponent = -exponent;
        }
        double result = 1;
        for(int i = 0; i < exponent; i++){
            result *=  base;
        }
        return result;
    }
};

题目描述

输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。

class Solution {
public:
    void reOrderArray(vector<int> &array) {
        int len = array.size();
        if(len <= 1){
            return;
        }
        vector<int> oddArray;
        vector<int> evenArray;
        for(int i = 0; i < len; i++){
            if(array[i] & 1 == 1){
                oddArray.push_back(array[i]);
            }else{
                evenArray.push_back(array[i]);
            }
        }
        array.clear();
        for(int i = 0; i < oddArray.size(); i++){
            array.push_back(oddArray[i]);
        }
        for(int i = 0; i < evenArray.size(); i++){
            array.push_back(evenArray[i]);
        }
    }
};

题目描述

输入一个链表,输出该链表中倒数第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) {
        ListNode* result = NULL;
        if(k > 0){
            ListNode* tempPoint = pListHead;
            while(tempPoint != NULL && --k > 0){
                tempPoint = tempPoint -> next;
            }
            if(tempPoint != NULL && k == 0){
                result = pListHead;
                tempPoint = tempPoint -> next;
                while(tempPoint!= NULL){
                    result = result -> next;
                    tempPoint = tempPoint -> next;
                }
            }
        }
        return result;
    }
};

题目描述

输入一个链表,反转链表后,输出新链表的表头。

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
            val(x), next(NULL) {
    }
};*/
class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
        ListNode* newHead = NULL;
        if(pHead != NULL){
            ListNode* current = pHead;
            ListNode* tempHead = pHead -> next;
            current -> next = NULL;
            newHead = current;
            while(tempHead != NULL){
                current = tempHead;
                tempHead = tempHead -> next;
                current -> next = newHead;
                newHead = current;
            }
        }
        return newHead;
    }
};

题目描述

输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

/*
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 == NULL){
            return pHead2;
        }
        if(pHead2 == NULL){
            return pHead1;
        }
        ListNode* newHead = pHead1;
        if(pHead1 -> val > pHead2 -> val){
            newHead = pHead2;
            pHead2 = pHead2 -> next;
        }else{
            pHead1 = pHead1 -> next;
        }
        ListNode* current = newHead;
        while(pHead1 != NULL && pHead2 != NULL){
            if(pHead1 -> val > pHead2 -> val){
                current -> next = pHead2;
                current = pHead2;
                pHead2 = pHead2 -> next;
            }else{
                current -> next = pHead1;
                current = pHead1;
                pHead1 = pHead1 -> next;
            }
        }
        while(pHead1 != NULL){
            current -> next = pHead1;
            current = pHead1;
            pHead1 = pHead1 -> next;
        }
        while(pHead2 != NULL){
            current -> next = pHead2;
            current = pHead2;
            pHead2 = pHead2 -> next;
        }
        return newHead;
    }
};

题目描述

输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)

/*
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(pRoot2 == NULL || pRoot1 == NULL){
            return false;
        }
        return compareSubTree(pRoot1,pRoot2);
    }
    bool compareSubTree(TreeNode* pRoot1, TreeNode* pRoot2){
        if(pRoot2 == NULL){
            return true;
        }
        if(pRoot1 == NULL){
            return false;
        }
        if(pRoot1 -> val == pRoot2 -> val){
            if (compareSubTree(pRoot1 -> left,pRoot2 -> left)&&compareSubTree(pRoot1 -> right,pRoot2 -> right)){
                return true;
            }
        }
        return compareSubTree(pRoot1 -> left,pRoot2)||compareSubTree(pRoot1 -> right,pRoot2);
    }
};

题目描述

操作给定的二叉树,将其变换为源二叉树的镜像。

输入描述:

二叉树的镜像定义:源二叉树 
    	    8
    	   /  \
    	  6   10
    	 / \  / \
    	5  7 9 11
    	镜像二叉树
    	    8
    	   /  \
    	  10   6
    	 / \  / \
    	11 9 7  5

/*
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 != NULL){
            queue<TreeNode*> tempQueue;
            tempQueue.push(pRoot);
            TreeNode* current = NULL;
            TreeNode* temp = NULL;
            while(!tempQueue.empty()){
                current = tempQueue.front();
                tempQueue.pop();
                if(current -> left == NULL && current ->right == NULL){
                    continue;
                }else{
                    if(current -> left != NULL){
                        tempQueue.push(current -> left);
                    }
                    if(current -> right != NULL){
                        tempQueue.push(current -> right);
                    }
                    temp = current -> right;
                    current -> right = current -> left;
                    current -> left = temp;
                }
            }
        }
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值