力扣刷题-946.验证栈序列、1275.找出井字棋的获胜者

946.验证栈序列

题目

题解

typedef struct Stack{
    int* data;
    int ptr;
}Stack;

bool validateStackSequences(int* pushed, int pushedSize, int* popped, int poppedSize){
    //栈初始化
    Stack s;
    s.data = (int*)malloc(sizeof(int)*pushedSize);
    s.ptr = -1;
    //不断进栈,每次进栈后不断匹配,如果匹配上了就出栈
    int pushPtr = 0;
    int popPtr = 0;
    while(pushPtr < pushedSize){
        s.data[++s.ptr] = pushed[pushPtr++];
        while(s.ptr!=-1 && s.data[s.ptr] == popped[popPtr]){
            s.ptr--;
            popPtr++;
        }
    }
    if(s.ptr == -1){
        return true;
    }
    return false;
}

要点

  1. 在匹配时要检测栈的指针是否已经等于-1了,防止栈溢出。

1275.找出井字棋的获胜者

题目

题解

char * tictactoe(int** moves, int movesSize, int* movesColSize){
    int AH[3]={0},AL[3]={0},AX[2]={0};
    int BH[3]={0},BL[3]={0},BX[2]={0};
    int i = 0;
    for(;i < movesSize;i++){
        if(i%2 == 0){//A
            //行
            AH[moves[i][0]]+=1;
            if(AH[moves[i][0]] == 3){
                char* s = "A";
                return s;
            }
            //列
            AL[moves[i][1]]+=1;
            if(AL[moves[i][1]] == 3){
                char* s = "A";
                return s;
            }
            //斜对角线
            if(moves[i][0]==moves[i][1]){
                AX[0] += 1;
                if(AX[0] == 3){
                    char* s = "A";
                    return s;
                }
            }
            //副对角线
            if((moves[i][0]==0&&moves[i][1]==2)||(moves[i][0]==1&&moves[i][1]==1)||(moves[i][0]==2&&moves[i][1]==0)){
                AX[1] += 1;
                if(AX[1] == 3){
                    char* s = "A";
                    return s;
                }
            }
        }else{//B
            //行
            BH[moves[i][0]]+=1;
            if(BH[moves[i][0]] == 3){
                char* s = "B";
                return s;
            }
            //列
            BL[moves[i][1]]+=1;
            if(BL[moves[i][1]] == 3){
                char* s = "B";
                return s;
            }
            //斜对角线
            if(moves[i][0]==moves[i][1]){
                BX[0] += 1;
                if(BX[0] == 3){
                    char* s = "B";
                    return s;
                }
            }
            //副对角线
            if((moves[i][0]==0&&moves[i][1]==2)||(moves[i][0]==1&&moves[i][1]==1)||(moves[i][0]==2&&moves[i][1]==0)){
                BX[1] += 1;
                if(BX[1] == 3){
                    char* s = "B";
                    return s;
                }
            }
        }
    }
    if(movesSize == 9){
        char* s = "Draw";
        return s;
    }
    char* s = "Pending";
    return s;
}

要点

  1. 预设A、B的行、列、对角线得分,当满足时即获胜。

  1. c语言字符串指针的赋值:char *s = "hello";

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东东咚咚东

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值