C++算法记录

5 篇文章 0 订阅

记录部分算法

typedef float ElemType;
typedef struct node{
    ElemType data;
    struct node* lchild;
    struct node* rchild;
    struct node* parent;
}BTNode;
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
        val(x), next(nullptr) {
    }
};
/**向二叉树中插入结点**/
void TreeNode::InsertIntoBitTree(BTNode **root,ElemType data)
{
    /*创建新结点保存待插入的data*/
    BTNode *newNode=static_cast<BTNode*>(malloc(sizeof(BTNode)));
    newNode->data=data;
    newNode->lchild=nullptr;
    newNode->rchild=nullptr;
    //考虑到 当输入是空树时 需要改变根结点(BiTNode *root)的值,所以这里要求输入根节点的地址(BiTNode **root类型)
    if(*root==nullptr)//说明是空树
        {
            *root=newNode;//将新结点的指针newNode赋值给根结点的指针
            (*root)->parent=nullptr;
            (*root)->lchild=nullptr;
            (*root)->rchild=nullptr;
        }
    else if((*root)->lchild==nullptr)
    {
        /*左子树为空 则将新结点newNode的指针赋值给左子树结点的指针*/
        (*root)->lchild=newNode;
        (*root)->lchild->parent=*root;//左子树的父节点为根结点

    }

    else if((*root)->rchild==nullptr)
    {
        /*右子树为空 则将新结点newNode的指针赋值给右子树结点的指针*/
        (*root)->rchild=newNode;
        (*root)->rchild->parent=*root;//右子树的父节点为根结点
    }

    /*如果根节点、左右子树都不为空 递归向左子树插入data*/
    /*这样构造的树的特点是:根结点的右子树只有一个结点*/
    else if(static_cast<int>(data) == 8 || static_cast<int>(data) == 6)
        InsertIntoBitTree(&((*root)->rchild),data);
    else
        InsertIntoBitTree(&((*root)->lchild),data);
}
/*中序遍历并输出:左 根结点 右*/
void TreeNode::MidPrint(BTNode *root)
{
    if(root==nullptr)
    {
        printf("invalid MidPrint");
        exit(-1);
    }
    if(root->lchild!=nullptr)
    MidPrint(root->lchild);
    qDebug() << root->data << "\n";
    if(root->rchild!=nullptr)
    MidPrint(root->rchild);
}

//中序遍历
void TreeNode::InOrderWithoutRecursion1(BTNode* root)
{
    //空树
    if (root == nullptr)
        return;
    //树非空
    BTNode* p = root;
    QStack<BTNode*> s;
    while (!s.empty() || p)
    {
        //一直遍历到左子树最下边,边遍历边保存根节点到栈中
        while (p)
        {
            s.push(p);
            p = p->lchild;
        }
        //当p为空时,说明已经到达左子树最下边,这时需要出栈了
        if (!s.empty())
        {
            p = s.top();
            s.pop();
            qDebug() << p->data;
            //进入右子树,开始新的一轮左子树遍历(这是递归的自我实现)
            p = p->rchild;
        }
    }
}
//后序遍历
void TreeNode::InOrderWithoutRecursion2(BTNode* root)
{
    //空树
    if (root == nullptr)
        return;
    //树非空
    BTNode* p = root,*pre = nullptr;
    QStack<BTNode*> s;
    s.push_back(p);
    
    while (!s.empty()){
        p = s.top();
        
        if(((p->lchild ==nullptr) && (p->rchild ==nullptr))  ||
                ((pre == p->lchild || pre == p->rchild) && pre != nullptr) ){
             
            qDebug() << p->data ;
            s.pop();
            pre = p;
        }
        else
        {
            if (p->rchild != nullptr)
                s.push(p->rchild);
            if (p->lchild != nullptr)
                s.push(p->lchild);
        }
    }
    
}
int TreeNode::treeDepth(BTNode *root)
{
    if(root == nullptr)
        return 0;
    int lnum = treeDepth(root->lchild);
    int rnum = treeDepth(root->rchild);
    return std::max(lnum,rnum) + 1;

}
//找到环链表的入口
ListNode *TreeNode::EntryNodeOfLoop(ListNode *pHead)
{
    ListNode *fast = pHead;
    ListNode *slow = pHead;
    while (fast && fast->next) {
        fast = fast->next->next;
        slow = slow->next;
        if(fast == slow) break;
    }
    if(!fast || !fast->next) return nullptr;
    fast = pHead;
    while (fast != slow) {
        fast = fast->next;
        slow = slow->next;
    }
    return fast;
}

    //单个字符出现位置
    int FirstNotRepeatingChar(std::string str);
    //最长相同字符
    QString maxlen(QString qstr1,QString qstr2);
    //循环左移
    std::string LeftRotateString(std::string str, int n);
    //数组中两个只出现一次的数
    void FindNumsAppearOnce(QVector<int> data,int* num1,int *num2);
    //奇偶分离(奇靠前)
    void reOrderArray(QVector<int> &array);
    //可走1阶和2阶,走n阶有几种走法
    int stepsNum(int nstep);
    //空格替换
    void replaceSpace(char *str,int length);
    bool Find(int target, QVector<QVector<int> > array);



int algorithmString::FirstNotRepeatingChar(std::string str){
    QMap<char,int> mp;
    for (const char ch : str) {
        mp.insert(ch,mp[ch]+1);
        //++mp[ch];
    }
    for (int i=0; i< str.length(); ++i) {
        if (mp[str[i]] == 1) return i;
    }
    return -1;
//    int num = 0;
//    int len = str.length();
//    if(len==0) return -1;
//    if(len==1) return 0;
//    bool flag[len];
//    for(int i=len-1;i>=0;i--){
//        for(int j=i+1;j<len;j++){
//            if(str[i]==str[j]){
//                flag[i] = true;
//                flag[j] = true;
//                break;
//            }
//            num++;
//        }
//    }
//    qDebug() << "num--"<<num;
//    for(int i=0;i<len;i++){
//        if(flag[i]==false)
//            return i;
//    }
//    return -1;
}
//求最大相同字符串
QString algorithmString::maxlen(QString qstr1,QString qstr2){
    QVector<QVector<int>> record(qstr1.length(),QVector<int>(qstr2.length()));
    int maxLen=0,maxEnd=0;
    for (int i=0; i<qstr1.length();i++) {
        for (int j=0; j<qstr2.length();j++) {
            if(qstr1[i] == qstr2[j]){
                if(i==0 || j==0){
                    record[i][j] = 1;
                }else {
                    record[i][j] = record[i-1][j-1] + 1;
                }
            }
            else {
                record[i][j] = 0;
            }
            if(record[i][j] > maxLen){
                maxLen = record[i][j];
                maxEnd = i;
            }
        }
    }
    return qstr1.mid(maxEnd - maxLen + 1, maxLen);
}

std::string algorithmString::LeftRotateString(std::string str, int n)
{
    if(n>str.length())return "";
    std::string rstr = "";
    for (int i=0;i<str.length()-n;i++) {
        rstr += str[n+i];
    }
    for (int i=n;i>0;i--) {
        rstr += str[n-i];
    }
    return rstr;
}
void algorithmString::FindNumsAppearOnce(QVector<int> data,int* num1,int *num2) {
    int m_num1=0,m_num2=0;
    QMap<int,int> num;
    for (int i=0;i<data.count();i++) {
        ++num[data[i]];
    }
    for (int i=0;i<data.count();i++) {
        if(num[data[i]] == 1 && m_num1 == 0)
            m_num1 = data[i];
        if(num[data[i]] == 1 && m_num1 != 0)
            m_num2 = data[i];
    }
    *num1 = m_num1;
    *num2 = m_num2;
}
void algorithmString::reOrderArray(QVector<int> &array) {
//    QVector<int> arr;
//    for (const int v : array) {
//        if (v&1) arr.push_back(v); // 奇数
//    }
//    for (const int v : array) {
//        if (!(v&1)) arr.push_back(v); // 偶数
//    }
//    std::copy(arr.begin(), arr.end(), array.begin());
    int Temp;
    int j=0;
    for (int i=0;i<array.size();i++) {
        if(array[i] & 1){
            if(i != j){
                Temp = array[i];
                for (int k=i;k>j;k--) {
                    array[k] = array[k-1];
                }
                array[j] = Temp;
                j++;
            }else {
                j++;
            }
        }
    }
}
int algorithmString::stepsNum(int nstep){
    if(nstep == 1)
        return 1;
    else if(nstep == 2)
        return 2;
    else if(nstep == 3)
        return 3;
    else {
        return stepsNum(nstep-1) + stepsNum(nstep - 2);
    }


    //时间复杂度O(n),空间复杂度O(n)
//    QVector<int> dp( nstep + 1, 0);
//        dp[1] = 1;
//        for (int i=2; i<=nstep; ++i) {
//            dp[i] = dp[i-1] + dp[i-2];
//        }
//        return dp[nstep];

}

void algorithmString::replaceSpace(char *str, int length)
{
    if(str == nullptr || length == 0) return;
    int cnt = 0;
    for (int i=0;i<length;i++) {
        if(str[i] == ' ') cnt++;
    }
    if(cnt == 0) return;

    int cntLength = length + cnt*2;
    for (int i=length;i>=0;i--) {
        if(str[i] == ' '){
            str[cntLength--] = '0';
            str[cntLength--] = '2';
            str[cntLength--] = '%';
        }
        else {
            str[cntLength--] = str[i];
        }
    }
}

bool algorithmString::Find(int target, QVector<QVector<int> > array)
{
 if (array.size() ==0 || array[0].size() ==0) return false;
     for (const auto& vec : array) {
         for (const int val : vec) {
             if (val == target)
                 return true;
         }
     }
     return false;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值