zoukankan      html  css  js  c++  java
  • 【leetcode】三合一

    /**
    *
    *整体思路:
    *1.定义一个结构体变量TripleInOne,存储模拟栈的数组,记录每个栈的起点位置的base数组,记录每个栈指针当前位置的top数组,以及每个栈的大小size成员
    *2.申请长度为1的TripleInOne型变量obj,申请长度为stacksize * 3的数组stack,申请长度为3的base和top数组,
    *3.初始化 obj -> base, obj -> top和obj -> size值,设置每个栈对应的起点位置
    *
    */
    
    typedef struct {
    
        //simulating stack
        int * stack;
        //the starting position of each stack
        int * base;
        //the current pointer of each stack
        int * top;
        //the length of each stack
        int size;
    
    } TripleInOne;
    
    
    TripleInOne * tripleInOneCreate( int stackSize ) {
    
        //alloclating memory to simulating stack
        TripleInOne * obj = ( TripleInOne * )malloc( sizeof( TripleInOne ) * 1 );
        obj -> stack = ( int * )malloc( sizeof( int ) * stackSize * 3 );
        obj -> top = ( int * )malloc( sizeof( int ) * 3 );
        obj -> base = ( int * )malloc( sizeof( int ) * 3 );
    
        //intializing the starting position of each stack and pointer
        obj -> top[ 0 ] = obj -> base[ 0 ] = 0;
        obj -> top[ 1 ] = obj -> base[ 1 ] = stackSize;
        obj -> top[ 2 ] = obj -> base[ 2 ] = stackSize * 2;
        obj -> size = stackSize;
    
        return obj;
    
    }
    
    /**
    *Function: pushing value to stack, if the stack if fulling, skipping current operation
    *@param: TripleInOne * obj : the simulating stack
    *@param: int stackNum : the sequence number of stack
    *Rerutn: void
    */
    void tripleInOnePush( TripleInOne * obj , int stackNum , int value ) {
    
        //checking the stack whether is fulling
        if( obj -> top[ stackNum ] < ( ( stackNum + 1 ) * obj -> size ) ) {
    
            obj -> stack[ obj -> top[ stackNum ] ] = value;
            obj -> top[ stackNum ] += 1;
    
        }
    
    }
    
    /**
    *Function: checking whether the stack is empty
    *@param: TripleInOne * obj : the simulating stack
    *@param: int stackNum : the sequence number of stack
    *Rerutn: true : is empty, false : is not empty
    */
    bool tripleInOneIsEmpty( TripleInOne * obj , int stackNum ) {
    
        return ( obj -> base[ stackNum ] == obj -> top[ stackNum ] );
    
    }
    
    /**
    *Function: popping the top element in stack
    *@param: TripleInOne * obj : the simulating stack
    *@param: int stackNum : the sequence number of stack
    *Rerutn: int : the value of the top element in stack , -1 : the stack is empty
    */
    int tripleInOnePop( TripleInOne * obj , int stackNum ) {
    
        if( tripleInOneIsEmpty( obj , stackNum ) ) {
    
            return -1;
    
        }
    
        //updating the pointer
        obj -> top[ stackNum ] -= 1;
    
        return obj -> stack[ obj -> top[ stackNum ] ];
    
    }
    
    /**
    *Function: getting the value of top element in stack
    *@param: TripleInOne * obj : the simulating stack
    *@param: int stackNum : the sequence number of stack
    *Rerutn: int : the value of the top element in stack , -1 : the stack is empty
    */
    int tripleInOnePeek( TripleInOne * obj , int stackNum ) {
    
        if( tripleInOneIsEmpty( obj , stackNum ) ) {
    
            return -1;
    
        }
    
        return obj -> stack[ obj -> top[ stackNum ] - 1 ];
    
    }
    
    /**
    *Function: freeing memory
    *@param: TripleInOne * obj : the simulating stack
    *Return: void
    */
    void tripleInOneFree( TripleInOne * obj ) {
    
        free( obj -> stack );
        free( obj -> base );
        free( obj -> top );
        free( obj );
    
    }
    
    /**
     * Your TripleInOne struct will be instantiated and called as such:
     * TripleInOne* obj = tripleInOneCreate(stackSize);
     * tripleInOnePush(obj, stackNum, value);
     
     * int param_2 = tripleInOnePop(obj, stackNum);
     
     * int param_3 = tripleInOnePeek(obj, stackNum);
     
     * bool param_4 = tripleInOneIsEmpty(obj, stackNum);
     
     * tripleInOneFree(obj);
    */
  • 相关阅读:
    数据库事务4种隔离级别及7种传播行为
    Spring AOP深入剖析
    SQL语句-创建索引
    global文件中的application_start方法中做: 定时器
    细说ASP.NET Forms身份认证
    asp.net(C#)实现功能强大的时间日期处理类完整实例
    C# 文件操作类大全
    SQL Server Profiler:使用方法和指标说明
    分库分表原则 总结
    做软件与团队建设——对带研发团队和管理的总结
  • 原文地址:https://www.cnblogs.com/ganxiang/p/13586107.html
Copyright © 2011-2022 走看看