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);
    */
  • 相关阅读:
    USASO Greedy Gift Givers
    Mat 类型用法
    OpenCV错误:Unhandled exception at 0x0133bc63 ....0xC0000005: Access violation reading location 0x00000004.
    C++ seekp 函数文件流跳转功能产生数据覆盖问题解决
    C++中文件名称必须是C风格的char*格式
    char*, char[] ,CString, string的转换
    Visual Stdio 2008 最大内存分配块大小问题: 使用new 分配连续723M内存 出错 std::bad_alloc at memory location 0x0013e0b8
    string类型转化为char*错误: error C2440: '=' : cannot convert from 'const char *' to 'char *'
    Mat 和 IplImage、CvMat格式的互相转换
    指针数组和数组指针
  • 原文地址:https://www.cnblogs.com/ganxiang/p/13586107.html
Copyright © 2011-2022 走看看