美文网首页
剑指offer

剑指offer

作者: 漆黑烈焰武士G | 来源:发表于2020-03-22 00:50 被阅读0次

二维数组中的查找

class Solution {
public:
    bool Find(int target, vector<vector<int> > array) 
    {
        if (array.size() <= 0 || array[0].size() <= 0)
            return false;
        
        int m = array.size() - 1;
        int n = 0;
        while(m >= 0 && n < array[0].size())
        {
            if (array[m][n] == target)
            {
                return true;
            }
            else if (array[m][n] > target)
            {
                m --;
            }
            else if (array[m][n] < target)
            {
                n ++;
            }
        }
        return false;
    }
};

替换空格

class Solution {
public:
    void replaceSpace(char *str,int length) 
    {
        if (str == NULL || length <= 0)
            return;
        
        int oldIndex = 0;
        int newIndex = 0;
        int blankNum = 0;
        
        while(str[oldIndex] != '\0')
        {
            if (str[oldIndex] == ' ')
            {
                blankNum ++;
            }
            oldIndex ++;
        }
        
        newIndex = oldIndex + blankNum *2;
        
        while(oldIndex >= 0 )
        {
            if (str[oldIndex] == ' ')
            {
                str[newIndex--] = '0';
                str[newIndex--] = '2';
                str[newIndex--] = '%';
            }
            else 
            {
                str[newIndex--] = str[oldIndex];
            }
            oldIndex --;
        }
    }
};

从尾到头打印链表

/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) 
    {
        vector<int> resualt;
        stack<int>  numStack;
        while (head)
        {
            numStack.push(head->val);
            head = head -> next;
        }
        while (numStack.size() > 0)
        {
            resualt.push_back(numStack.top());
            numStack.pop();
        }
        return resualt;
    }
};

重建二叉树

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) 
    {
        TreeNode* tree;
        if (pre.size() <= 0)
        {
            tree = NULL;
        }
        else if (pre.size() == 1)
        {
            tree = new TreeNode(pre[0]);
        }
        else
        {
            vector<int> leftPre;
            vector<int> leftVin;
            vector<int> rightPre;
            vector<int> rightVin;
            
            bool didFind = false;
            int currentIndex = 0;
        
            while (currentIndex < vin.size())
            {
                if (vin[currentIndex] != pre[0])
                {
                    if (!didFind)
                    {
                        leftVin.push_back(vin[currentIndex]);
                    }
                    else
                    {
                        rightVin.push_back(vin[currentIndex]);
                    }
                }
                else
                {
                    didFind = true;
                }
                currentIndex ++;
            }
            
            for (int i = 1;i < pre.size();i ++)
            {
                if (i <= leftVin.size())
                {
                    leftPre.push_back(pre[i]);
                }
                else
                {
                    rightPre.push_back(pre[i]);
                }
            }
            tree = new TreeNode(pre[0]);
            tree -> left = reConstructBinaryTree(leftPre,leftVin);
            tree -> right = reConstructBinaryTree(rightPre,rightVin);
        }
        return tree;
    }
};

相关文章

  • 全网最全剑指offer题目解答

    【剑指offer】Java版代码(完整版) 【剑指offer】1-10题 【剑指offer】11-20题 【剑指o...

  • 剑指offer 和 leetcode题目

    剑指offer leetcode

  • 单例模式

    单例模式 最近在看《剑指offer》,根据《剑指offer》的讲解,结合《effectiveJava》简单学习了一...

  • 年龄排序

    摘抄资料:《剑指offer》

  • 剑指offer

    第一题:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,...

  • 《剑指offer》

    4.调整数组顺序使奇数位于偶数前面 题目描述:输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇...

  • 剑指offer

    二维数组中查找数在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到...

  • 剑指offer

    剑指offer题目 1 一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。...

  • 剑指offer

    1.链表反转 1、输出倒数第k个数

  • 剑指Offer

    S1 S2 s3 s4 s5 s6 s7 s8 s9 s10 s11 s12 s13 s14 s15 s16 s1...

网友评论

      本文标题:剑指offer

      本文链接:https://www.haomeiwen.com/subject/kpopyhtx.html