zoukankan      html  css  js  c++  java
  • 算法面试题解答(四)

    1. 求两个字符串的最大公共字符串

    #include <iostream>

    using namespace std;

    void getLength(const char *src1, const char *src2, int *l1, int *l2)
    {
        if(src1 == NULL || src2 == NULL || strlen(src1) == 0 || strlen(src2) == 0)
        {
            *l1 = 0;
            *l2 = 0;
            return ;
        }
        int pl1 = 0,pl2 = 0;
        
        int tpl1 = 0,tpl2 = 0;
        int tpl3 = 0,tpl4 = 0;
        if(src1[0] == src2[0])
        {
            getLength(src1+1,src2+1, &tpl1, &tpl2);
            pl1 = tpl1+1;
        }
        else
        {
            pl1 = 0;
            getLength(src1+1,src2, &tpl1, &tpl2);
            getLength(src1,src2+1, &tpl3, &tpl4);
        }
        *l1 = pl1;
        *l2 = tpl2>tpl4?tpl2:tpl4;
        if(*l2<pl1)
            *l2 = pl1;
    }

    int main(int argc, char **argv)
    {
        int l1 =0 , l2  =0 ;
        getLength("abcdefg","cdefbcderg", &l1, &l2);
        cout<<l2<<endl;
    }

    2. Convert a binary search tree to a circular sorted linked list. The highest valued node should point to the lowest valued node at each step. 下面只是把BST转为单向链表,而且没有处理left指针,可以再完善一下。

    #include <iostream>
    using namespace std;
    struct BSTNode 
    {
        BSTNode(int v){value = v;left = right = NULL;}
        int value;
        BSTNode *left;
        BSTNode *right;
    };

    void ConvertBST2CiruluarLinkedList(BSTNode *root, BSTNode **head, BSTNode **tail)
    {
        if(root == NULL)
            return;
        BSTNode *headl = NULL, *taill = NULL;
        if(root->left != NULL)
        {
            ConvertBST2CiruluarLinkedList(root->left,&headl, &taill);        
        }
        if(headl != NULL)
        {
            *head = headl;
            taill->right = root;
        }
        else
        {
            *head = root;
        }
        BSTNode *headr = NULL, *tailr = NULL;
        if(root->right !=NULL)
        {
            ConvertBST2CiruluarLinkedList(root->right,&headr, &tailr);
        }
        if(headr != NULL)
        {
            root->right = headr; //anyway, we should use root to make connection with right linked list;
            tailr->right = *head;
            *tail = tailr;
        }
        else
        {
            *tail = root;
            (*tail)->right = *head;
        }
    }

    int main(int argc, char **argv)
    {
        BSTNode *a4 = new BSTNode (4);
        BSTNode *a3 = new BSTNode (3);
        BSTNode *a2 = new BSTNode (2);
        BSTNode *a1 = new BSTNode (1);
        BSTNode *a5 = new BSTNode (5);
        a2->left = a1;
        a2->right = a3;
        a4->left = a2;
        a4->right = a5;
        BSTNode * head = NULL, *tail = NULL;
        ConvertBST2CiruluarLinkedList(a4,&head,&tail);
    }
  • 相关阅读:
    tp5 查询问题 字段自增 字段比较
    七牛云 {"error":"no such domain"}
    mac 命令
    跟微信公众号一起来学api安全
    vue 运行别人项目
    php sha1withrsa
    thinkphp5 使用路由下分页 form表单 搜索
    P2134 百日旅行 (斜率优化,DP)
    [USACO Section 4.4]追查坏牛奶Pollutant Control (最小割)
    [HAOI2007] 理想的正方形 (单调队列)
  • 原文地址:https://www.cnblogs.com/whyandinside/p/2789163.html
Copyright © 2011-2022 走看看